Rumah > Soal Jawab > teks badan
P粉4208682942023-09-04 11:51:50
Untuk lebih banyak item data dan banyak lagi id
,我会选择一种创建Set
通过 map
ping 每个 itemList
项目的id
...
const idLookup = new Set(itemList.map(({ id }) => id));
Terus dari 集合
或Map
实例比例如更快通过 一次又一次迭代数组查找
或 包含
外部过滤
misi.
Menapis item yang tiada tandingan-id
senarai semudah...
const listOfNonMatchingIds = idList.filter(id => !idLookup.has(id));
...contoh kod...
const itemList = [ { id: 60, itemName: 'Main Location - 1100 Superior Road - Cleveland' }, { id: 1456, itemName: 'Third Location - 107,West 20th Street,Manhattan - New York' }, ]; const idList = [60, 1453, 1456]; const idLookup = new Set(itemList.map(({ id }) => id)); const listOfNonMatchingIds = idList.filter(id => !idLookup.has(id)); console.log({ listOfNonMatchingIds });
.as-console-wrapper { min-height: 100%!important; top: 0; }
P粉1300978982023-09-04 00:29:22
Boleh guna .map()
,然后使用 .filter()
和 .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]