我要2个数组tooarray
和moarray
是moarray
而不是tooarray
的所有元素的列表。
var tooarray = [
{ catalogue_id: 23480,
parent_category_id: 23479,
user_id: 28434,
form_id: 4261,
name: 'Other'
},
{ catalogue_id: 23481,
parent_category_id: 23479,
user_id: 28434,
form_id: 4261,
name: 'Tincture',
description: 'Tincture'
},
{ catalogue_id: 23482,
parent_category_id: 23479,
user_id: 28434,
form_id: 4261,
name: 'Cannabis',
description: 'Cannabis'
},
]
var moarray = [
'wedwewdwe',
'Other',
'Tincture',
'Cannabis'
];
var tInsertArray = moarray.filter(x =>{
tooarray.filter(y=>{
return x.indexOf(y.name) < 0;
})
});
输出接收
tInsertArray []
预期输出
tInsertArray [wedwewdwe]
最佳答案
从moarray
过滤所有元素,其中tooarray
中没有元素的name
属性等于每个moarray
条目:
let tooarray = [{
catalogue_id: 23480,
parent_category_id: 23479,
user_id: 28434,
form_id: 4261,
name: 'Other'
},
{
catalogue_id: 23481,
parent_category_id: 23479,
user_id: 28434,
form_id: 4261,
name: 'Tincture',
description: 'Tincture'
},
{
catalogue_id: 23482,
parent_category_id: 23479,
user_id: 28434,
form_id: 4261,
name: 'Cannabis',
description: 'Cannabis'
},
]
let moarray = [
'wedwewdwe',
'Other',
'Tincture',
'Cannabis'
];
let tInsertArray = moarray.filter(x => !tooarray.filter(y => y.name === x).length)
console.log(tInsertArray)
关于javascript - 过滤2个数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49221006/