I have 2 arrays, one is the items list and second one is a selected items list:
const orig = [
{
id: "1",
name: "First",
price: "100",
qty: "1",
sum: 1
},
{
id: "2",
name: "Second",
price: "100",
qty: "1",
sum: 1
},
{
id: "3",
name: "Third",
price: "99",
qty: "1",
sum: 1
}
];
const selected = [
{
id: "1",
name: "First",
price: "100",
qty: "1",
sum: 1
}
// {
// id: "2",
// name: "Second",
// price: "100",
// qty: "1",
// sum: 1
// }
];
I can loop over 2 arrays at the same time:
const arr = [];
orig.forEach((o) => {
selected.forEach((s) => {
if (o.id !== s.id) {
arr.push(o);
}
});
});
console.log(arr);
It works fine, when selected array has only 1 item, but if selected array got at least 2 items (second item is commented) then it doesn't work. Main purpose of this code is to remove selected items from orig array.
orig.filter(o => !selected.find(s => s.id === o.id))