【发布时间】:2020-10-28 01:15:32
【问题描述】:
假设我有 2 个多维对象数组
const products = [
{
id: 1
name: 'lorem'
},
{
id: 3,
name: 'ipsum'
}
];
const tmp_products = [
{
id: 1
name: 'lorem'
},
{
id: 14,
name: 'porros'
},
{
id: 3,
name: 'ipsum'
},
{
id: 105,
name: 'dolor'
},
{
id: 32,
name: 'simet'
}
];
通过id 属性查找缺失索引的正确方法是什么?
我期待像[1,3,4] 这样的输出,因为products 中不存在这些对象
我发现了一个类似的问题,但适用于普通数组: Javascript find index of missing elements of two arrays
var a = ['a', 'b', 'c'],
b = ['b'],
result = [];
_.difference(a, b).forEach(function(t) {result.push(a.indexOf(t))});
console.log(result);
我想使用 ES6 或 lodash 来尽可能短
【问题讨论】:
-
不,我需要缺少索引而不是缺少 ID,所以我期待
[1,3,4]
标签: javascript arrays indexing ecmascript-6