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]
3
Answers
You could create an IDs list of the existing data IDs using
.map()
, and then use.filter()
and.includes()
which will return an Array of all the Object IDs (from
data
) not listed in your referenceids
Array.@Roko’s solution is absolutely correct and probably a lot faster. Here’s my alternative approach using
.reduce()
and.some()
:For a grater amount of data-items and a grater amount of
id
s I would choose an approach which creates aSet
based lookup viamap
ping each of anitemList
item’sid
…Looking up items directly from a
Set
or aMap
instance is much faster than e.g. iterating again and again an array viafind
orincludes
for each iteration step of an outerfilter
task.Filtering a list of non matching item-
id
s then is as straightforward as …… example code …