i am trying to do the following, with several reads here and on the web but can’t seem to get it to work. I have two array that consist out of objects (filled dynamically) by two webservices, so I created a timed trigger and loop over them to update the products array.
var products= [
{idProduct: '1', price: '4.99'},
{idProduct: '2', price: '9.99'},
{idProduct: '3', price: '7.95'},
{idProduct: '4', price: '4.95'}
];
var update = [
{idProduct: '1', price: '4.99'},
{idProduct: '4', price: '4.95'}
];
what I would like to do is to remove the product from products array that are not in update. I know how to compare and how to add the product but that all checks from the update section. What I can’t seem to get to work is to delete the ones that are no longer in the update.
var i = 0;
var entry1;
while (i < products.length) {
entry1 = products[i];
if (update .some(function(entry2) { return entry1.ProductId === entry2.ProductId; })) {
// Found, progress to next
++i;
} else {
// Not found, remove
products.splice(i, 1);
}
}
Once processed I will remove the contents of update array and it story starts again.
update = []
Desired result of products Array:
products= [
{idProduct: '1', price: '4.99'},
{idProduct: '4', price: '4.95'}
];
and no, I can’t just use update array as this is a simplified example and part of a bigger code 🙂