Problem: I have an object that has a field call modules that is and array of activities. Activities is itself a complex and many nested object. I need to hunt for activities by their IDs and replace, delete or insert between existing objects
// Replace object in collection
Object.keys(data.modules).forEach(modKey => {
Object.keys(data.modules[modKey].activities).forEach(activityKey => {
if (data.modules[modKey].activities[activityKey].id === newActivity.id) {
data.modules[modKey].activities.splice(activityKey, 1, newActivity);
}
});
});
// Remove object in collection
Object.keys(data.modules).forEach(modKey => {
Object.keys(data.modules[modKey].activities).forEach(activityKey => {
if (
data.modules[modKey].activities[activityKey] &&
data.modules[modKey].activities[activityKey].id === newActivity.id
) {
data.modules[modKey].activities.splice(activityKey, 1);
}
});
});
// Insert object at index
const moudleIndex = 0;
const activityIndex = 2;
Object.keys(data.modules).forEach(modKey => {
if (modKey == moudleIndex) {
Object.keys(data.modules[modKey].activities).forEach(activityKey => {
if (activityKey == activityIndex) {
data.modules[modKey].activities.splice(activityKey, 0, newActivity);
}
});
}
});