I need to filter out specific items from an array (testCopy) while also removing the corresponding items in other arrays (testCopy2 and testCopy3). For example, I want the filtered result to be testCopy [0, 2], testCopy2 [1,3], and testCopy3 [3,5]. Essentially, I want to remove the same items from the other two arrays that were removed from the index being filtered. I attempted to slice them off by using the filter but it didn't work as expected. Do you have any suggestions on how to achieve this?
let test = [0, 1, 2, 4, 5];
let test2 = [1, 2, 3, 6, 5];
let test3 = [3, 4, 5, 8, 9];
let testCopy = [...test];
let testCopy2 = [...test2];
let testCopy3 = [...test3];
let testClone = testCopy.filter((item, index) => {
if (item === 0 || item === 2) {
return true;
} else {
testCopy2.splice(index, 1);
testCopy3.splice(index, 1);
return false
}
});
console.log(testClone, testCopy2, testCopy3); // [0, 2], [1, 3, 6], [3, 5, 8]