I am facing an issue with updating a global array that contains objects, where each object includes another array. My goal is to update the main array with values from the arrays within the objects following a specific logic!
generalArray = [{name:String, features:String[]}]
// Here's the code snippet
let array1 = [{ name: "num", features: ['id'] },
{ name: "cat", features: ['gender'] }];
ob = {name:'num2', features:['id']};
function updateArr(arr, ob) {
const index = arr.findIndex(x =>
ob.features.toString() === x.features.toString()
);
if (index === -1) {
arr.push(ob);
} else {
arr[index] = ob;
}
}
console.log(array1);
updateArr(array1, ob);
console.log(array1);
The current solution works flawlessly when each object's 'features' array contains only one string. However, it encounters an issue when an 'features' array includes more than one string. For example, if 'features'=['id','gender'], the function fails to perform as expected. Any assistance would be greatly appreciated! Thank you.