Need help with removing specific items from an array within objects?
If you want to delete all hobbies related to dancing, you may consider using the splice method
const people = [{
id: 1,
documents: [{
id: 1,
category: [{
hobby: 'soccer'
},
{
hobby: 'dance'
}
]
}, {
id: 2,
category: [{
hobby: 'soccer'
},
{
hobby: 'dance'
}
]
}]
}, {
id: 2,
documents: [{
id: 1,
category: [{
hobby: 'dance'
},
{
hobby: 'soccer'
}
]
}, {
id: 2,
category: [{
hobby: 'soccer'
},
{
hobby: 'dance'
}
]
}]
}];
// Loop through each person
people.forEach(person => {
person.documents.forEach(document => {
document.category.forEach((cat, index) => {
if (cat.hobby === 'dance') {
// Use splice to remove dance hobby
document.category.splice(index, 1);
}
});
});
});
console.log(people);