Home > Back-end >  How can I count and the number of times a property from one array appears in another, and map the ma
How can I count and the number of times a property from one array appears in another, and map the ma

Time:01-24

I am trying to match the fk_city to the $id of the object in 'list_cities', then count the number of times they occur.

const list_cities = [
  { $id: '15FG', name: 'Pittsburg' },
  { $id: '50HS', name: 'Los Angeles' },
];

const list_places = [
  {
    $id: '59HE',
    fk_city: '15FG',
    name: 'Some Place',
    description: 'I have text here',
    slug: 'some-place',
  },
  {
    $id: '94KR',
    fk_city: '15FG',
    name: 'Another Place',
    description: 'This is the description',
    slug: 'another-place',
  },
  {
    $id: '05HE',
    fk_city: '50HS',
    name: 'The last Place',
    description: 'More text here',
    slug: 'the-last-place',
  },
];

I am basically trying to count the number of places in each city, but so far i can't get it to output a new array that just shows:

[
    {city: "Pittsburg", places: "2"}
    {city: "Los Angeles", places: "1"}
]  

I've tried matching the $id from list_cities to the property fk_city in list_places, but I don't know how to then count the times the same $id appears in the places array. This is supposed to be a foreign key relation, but the backend I use can only serve the id's raw like this from the endpoint so i need to manually map the $id's.

Any help is appreciated! Thanks!

CodePudding user response:

Try to loop list_cities and count the number of occurrences in list_places:

const list_cities = 
    [
     {$id: "15FG", name: "Pittsburg"},
     {$id: "50HS", name: "Los Angeles"}
    ]
    
    const list_places = [
     {$id: "59HE", fk_city: "15FG", name: "Some Place", description: "I have text here", slug: "some-place"},
     {$id: "94KR", fk_city: "15FG", name: "Another Place", description: "This is the description", slug: "another-place",},
     {$id: "05HE", fk_city: "50HS", name: "The last Place" , description: "More text here", slug: "the-last-place"}
    ] 
    
const cityCount = [];
for (const c of list_cities) {
    cityCount.push({
    city: c.name,
    places: list_places.filter(p => p.fk_city === c.$id).length
  })
}
console.log(cityCount);

CodePudding user response:

If list_places is long you can avoid multiple iterations of it by first creating a lookup table by fk_city and then map your cities against it.

const list_cities = [{ $id: '15FG', name: 'Pittsburg' }, { $id: '50HS', name: 'Los Angeles' },];
const list_places = [{ $id: '59HE', fk_city: '15FG', name: 'Some Place', description: 'I have text here', slug: 'some-place', }, { $id: '94KR', fk_city: '15FG', name: 'Another Place', description: 'This is the description', slug: 'another-place', }, { $id: '05HE', fk_city: '50HS', name: 'The last Place', description: 'More text here', slug: 'the-last-place', },];

const placeCount = {};
for (const { fk_city } of list_places) {
  placeCount[fk_city] = (placeCount[fk_city] ?? 0)   1;
}

const result = list_cities.map((city) => ({
  ...city,
  placeCount: placeCount[city.$id],
}));

console.log(result);

CodePudding user response:

This code works for me, Please check it can help you

list_cities.forEach((resp) => {
  list_places.forEach((res) => resp.$id === res.fk_city && (resp.places ? (resp.places = resp.places   1): (resp.places = 1))) 
  return resp
});

const city =  list_cities.map((resp) => {
  return {city: resp.name, places: resp.places}
});
console.log(city); // use this variable
  • Related