I have the following object and array of numbers. How can I tell which number in the array does not exist as an id in the object? In the following example, I would want 1453.
[
{id: 60, itemName: 'Main Location - 1100 Superior Road - Cleveland'}
{id: 1456, itemName: 'Third Location - 107,West 20th Street,Manhattan - New York'}
]
[60, 1453, 1456]
You could create an IDs list of the existing data IDs using .map()
, and then use .filter()
and .includes()
const data = [
{id: 60, itemName: 'Main Location - 1100 Superior Road - Cleveland'},
{id: 1456, itemName: 'Third Location - 107,West 20th Street,Manhattan - New York'}
]
const ids = [60, 1453, 1456];
const dataIDs = data.map(ob => ob.id); // [60, 1456]
const notExistentIDs = ids.filter(id => !dataIDs.includes(id));
console.log(notExistentIDs); // [1453]
which will return an Array of all the Object IDs (from data
) not listed in your reference ids
Array.