mr_anastasov

population

Apr 27th, 2023
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export default function (data) {
  2.     let cities = [...data.cities];
  3.     let people = [...data.people];
  4.  
  5.     const citiesWithPopulation = cities.reduce((city, elem) => {
  6.  
  7.         const id = elem.id;
  8.         let obj = {name: elem.name, id: elem.id, citizens: [], population: 0};
  9.  
  10.         people.map((per) => {
  11.             if(id === per.city) {
  12.                 obj.citizens.push(per.name);
  13.             }
  14.         });
  15.  
  16.         obj.population = obj.citizens.length;
  17.  
  18.         city.push(obj);
  19.  
  20.         return city;
  21.     }, [])
  22.  
  23.     const peopeUpdated = people.reduce((arr, elem) => {
  24.  
  25.     let person = {name: elem.name, city: {}};
  26.  
  27.     citiesWithPopulation.map (el => {
  28.         if (el.id === elem.city) {
  29.             person.city = {name: el.name, population: el.population};
  30.         }
  31.         })
  32.  
  33.         arr.push(person);
  34.  
  35.         return arr;
  36.     },[])
  37.  
  38.     return peopeUpdated;
  39.  
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment