请问大家,这个小算法怎么处理
var arr1 = ['27709', '29406', '29407', '29408', '29409', '29410', '29411', '29412', '29414', '29415']var arr2 = [
{
"goods_id": "29416",
"goods_sn": "10088011100050",
"goods_number": "105",
},
{
"goods_id": "29417",
"goods_sn": "10088011100059",
"goods_number": "0",
},
{
"goods_id": "29420",
"goods_sn": "10088011100070",
"goods_number": "0",
},
{
"goods_id": "29424",
"goods_sn": "10088011100081",
"goods_number": "0",
},
{
"goods_id": "29425",
"goods_sn": "JX10088011100085",
"goods_number": "0",
},
{
"goods_id": "29426",
"goods_sn": "10088011100090",
"goods_number": "0",
},
{
"goods_id": "29427",
"goods_sn": "10088011100092",
"goods_number": "0",
},
{
"goods_id": "29428",
"goods_sn": "10088011100094",
"goods_number": "0",
},
{
"goods_id": "29429",
"goods_sn": "10088011100095",
"goods_number": "0",
},
{
"goods_id": "29430",
"goods_sn": "10088011100096",
"goods_number": "0",
},
{
"goods_id": "29416",
"goods_sn": "10088011100050",
"goods_number": "0",
},
{
"goods_id": "29417",
"goods_sn": "10088011100059",
"goods_number": "0",
},
{
"goods_id": "29420",
"goods_sn": "10088011100070",
"goods_number": "0",
},
{
"goods_id": "29424",
"goods_sn": "10088011100081",
"goods_number": "0",
},
{
"goods_id": "29425",
"goods_sn": "10088011100085",
"goods_number": "0",
},
{
"goods_id": "29426",
"goods_sn": "10088011100090",
"goods_number": "0",
},
{
"goods_id": "29427",
"goods_sn": "10088011100092",
"goods_number": "0",
},
{
"goods_id": "29428",
"goods_sn": "10088011100094",
"goods_number": "0",
},
{
"goods_id": "29429",
"goods_sn": "10088011100095",
"goods_number": "0",
},
{
"goods_id": "29430",
"goods_sn": "10088011100096",
"goods_number": "0",
}
]
上面两个数组,
我想根据arr1数组里的数字,去和arr2中的goods_id 进行匹配,如果arr1的数组数字有和arr2中的goods_id 相等的话,就过滤掉这些相等的数据,返回出不相等数据,请问该怎么写,
回答:
var arr3 = arr1.filter((item) => !arr2.some((i) => i.goods_id === item));console.log(arr3);
回答:
function filter(data, id) { var ret = []
loop: for (var i = 0; i < data.length; ++i) {
for (var j = 0; j < id.length; ++j) {
if (data[i].goods_id === id[j]) {
continue loop;
}
}
ret.push(data[i]);
}
return ret;
}
console.dir(filter(arr2, arr1));
回答:
用arr2过滤arr1
arr1.filter(id => !arr2.some(item => item.goods_id === id))
回答:
var arr3 = new Set(arr1)arr2.filter(i => !arr3.has(i.goods_id))
以上是 请问大家,这个小算法怎么处理 的全部内容, 来源链接: utcz.com/p/937380.html