Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export default function (data) {
- let cities = [...data.cities];
- let people = [...data.people];
- const citiesWithPopulation = cities.reduce((city, elem) => {
- const id = elem.id;
- let obj = {name: elem.name, id: elem.id, citizens: [], population: 0};
- people.map((per) => {
- if(id === per.city) {
- obj.citizens.push(per.name);
- }
- });
- obj.population = obj.citizens.length;
- city.push(obj);
- return city;
- }, [])
- const peopeUpdated = people.reduce((arr, elem) => {
- let person = {name: elem.name, city: {}};
- citiesWithPopulation.map (el => {
- if (el.id === elem.city) {
- person.city = {name: el.name, population: el.population};
- }
- })
- arr.push(person);
- return arr;
- },[])
- return peopeUpdated;
- }
Advertisement
Add Comment
Please, Sign In to add comment