Children could be born in the same city and so find() is not particularly well-suited for this problem as it only returns the first result its looking for. In this case filter() is the function we need.
Below, we call filter inside of reduce(). This is different from the other answer as it does not iterate thru your input data more than once. The other answer will first create an entire array of children, then in a second iteration, select the ones that match your city query.
Alternatively, this answer iterates thru each president once and adds any matching children to the result along the way.
const findByCity = (city, data = {}) =>
data .reduce
( (result, { children = [] }) =>
result .concat (children .filter (c => c.city === city))
, []
)
In your input data, each child has a unique city, so it doesn't yield a good demonstration. I've added some more children born in city5 so we can see an answer that includes multiple children
const data =
[ { name: 'Trump'
, children:
[ { name: 'Trump Child 1', city: 'city1' }
, { name: 'Trump Child 2', city: 'city2' }
]
}
, { name: 'Barack Obama'
, children:
[ { name: 'Barack Obama Child 1', city: 'city3' }
, { name: 'Barack Obama Child 2', city: 'city4' }
]
}
, { name: 'Clinton'
, children:
[ { name: 'Clinton Child 1', city: 'city5' }
, { name: 'Clinton Child 2', city: 'city6' }
]
}
, { name: 'Bush'
, children:
[ { name: 'Bush Child 1', city: 'city5' }
, { name: 'Bush Child 2', city: 'city5' }
]
}
]
console .log (findByCity ('city1', data))
// [ { name: 'Trump Child 1', city: 'city1' } ]
console .log (findByCity ('city5', data))
// [ { name: 'Clinton Child 1', city: 'city5' }
// , { name: 'Bush Child 1', city: 'city5' }
// , { name: 'Bush Child 2', city: 'city5' }
// ]
Another answer provided here would break if your data contained a children-less president. findByCity does not suffer from this problem
const data =
[ // ...
, { name: 'Bush'
, children:
[ { name: 'Bush Child 1', city: 'city5' }
, { name: 'Bush Child 2', city: 'city5' }
]
}
// watch out for child-less presidents!
, { name: 'Polk' }
]
Expand the program below to run it in your browser
const findByCity = (city, data = {}) =>
data .reduce
( (result, { children = [] }) =>
result .concat (children .filter (c => c.city === city))
, []
)
const data =
[ { name: 'Trump'
, children:
[ { name: 'Trump Child 1', city: 'city1' }
, { name: 'Trump Child 2', city: 'city2' }
]
}
, { name: 'Barack Obama'
, children:
[ { name: 'Barack Obama Child 1', city: 'city3' }
, { name: 'Barack Obama Child 2', city: 'city4' }
]
}
, { name: 'Clinton'
, children:
[ { name: 'Clinton Child 1', city: 'city5' }
, { name: 'Clinton Child 2', city: 'city6' }
]
}
, { name: 'Bush'
, children:
[ { name: 'Bush Child 1', city: 'city5' }
, { name: 'Bush Child 2', city: 'city5' }
]
}
, { name: 'Polk' }
]
console .log (findByCity ('city1', data))
// [ { name: 'Trump Child 1', city: 'city1' } ]
console .log (findByCity ('city5', data))
// [ { name: 'Clinton Child 1', city: 'city5' }
// , { name: 'Bush Child 1', city: 'city5' }
// , { name: 'Bush Child 2', city: 'city5' }
// ]
{Name: 'TrumpChild1', City: 'city1'}?