In scenarios where children are born in the same city, using find()
may not be the best approach as it only retrieves the first result it finds. Instead, we can utilize filter()
.
This solution incorporates calling filter
within reduce()
. This method differs from other solutions by minimizing iterations through the input data. The alternative approach presented here processes each president once and adds any matching children along the way.
const findByCity = (city, data = {}) =>
data .reduce
( (result, { children = [] }) =>
result .concat (children .filter (c => c.city === city))
, []
)
Considering that each child in the input data has a unique city, this example may not provide a clear demonstration. To showcase results with multiple children sharing the same birth city of city5
, additional entries have been included.
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' }
// ]
An important distinction is how another solution might fail if data includes a president without any children listed. In contrast, the findByCity
function avoids encountering such issues.
const data =
[ // ...
, { name: 'Bush'
, children:
[ { name: 'Bush Child 1', city: 'city5' }
, { name: 'Bush Child 2', city: 'city5' }
]
}
// Caution required for presidents with no children!
, { name: 'Polk' }
]
Feel free to expand on the provided program code and run it within your browser environment.
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' }
// ]