Reputation: 11
I am trying to create such function in JS:
const removeObjectFromArray = (id, array) => {
const newArray = array.filter((item) => item.id !== id);
array = newArray;
}
Everything would be great but the last line is not working. I want it to refer to array name (in this case incomeArray) and overwite the variable but it doesn't work this way.
Can you please help me finding a solution?
Upvotes: 0
Views: 50
Reputation: 3364
You can just return the value and use it that way.
const removeObjectFromArray = (id, array) => {
return array.filter((item) => item.id !== id);
}
let myArray = [{id:5}, {id:10}];
myArray = removeObjectFromArray( 10, myArray);
// myArray is now [{id:5}]
Upvotes: 4