Within my code, there is a kids
object structured as follows:
const kids = {
name: 'john',
extra: {
city: 'London',
hobbies: [
{
id: 'football',
team: 'ABC',
},
{
id: 'basketball',
team: 'DEF',
},
],
},
};
In addition to that, I have an object containing various sports along with additional details for each sport.
const sports = [
{
name: 'volleyball',
coach: 'tom',
},
{
name: 'waterpolo',
coach: 'jack',
},
{
name: 'swimming',
coach: 'kate',
},
{
name: 'football',
coach: 'sara',
},
];
The objective here is to extract all the id
s from the hobbies array and iterate through each item in the sports array. If a match is found between a hobby and a sport, an additional field available
with a value of true
should be added to the sport object. Also, include its corresponding team name. The expected result should resemble this:
const result = [
{
name: 'volleyball',
coach: 'tom',
},
{
name: 'waterpolo',
coach: 'jack',
},
{
name: 'swimming',
coach: 'kate',
},
{
name: 'football',
coach: 'sara',
available: true, // it exists in kids' hobbies
team: 'DEF' // get it from kids' hobbies
},
];
Here's an attempt that has been made:
const result = kids.extra.hobbies.map(a => a.id);
for (var key in sports) {
console.log(sports[key].name);
const foundIndex = result.indexOf(sports[key].name);
if ( foundIndex > -1) {
sports[key].available = true;
}
}
console.log(sports)
However, this doesn't account for including the team information. How can this be incorporated into the existing code?