I am faced with the challenge of manipulating a JSON array by applying certain conditions for removal:
var data = [
{data1: "aaa", data2: "bbb", data3: "ccc"}, // First
{data1: "ddd", data2: "eee", data3: "fff"}, // Second
{data1: "ggg", data2: "hhh", data3: "iii"}, // Third
{data1: "jjj", data2: "eee", data3: "lll"} // Fourth
];
angular.forEach(data, (item, i) => {
if (item.data2 == 'eee' || item.data3 == 'iii') {
data.splice(i, 1);
}
});
console.log(data);
In the scenario above, my goal is to eliminate the second (due to data2 containing "eee"), third (due to data3 containing "iii"), and fourth (since data2 is "eee") objects from the data array. However, I have encountered an issue where the third object is not removed and remains in the data array.
I am questioning whether using the OR (||) operator in this way is correct. If not, what is the proper method for removing elements from an array based on multiple conditions?
I have spent numerous hours trying to solve this problem, but it seems that I might be overlooking something.