[英]How to get get not matching objects from two json array in javascript
I have two JSON arrays like this:我有两个 JSON arrays 像这样:
var modelType = [
{ 'id' : 3, 'name': 'eR_Beta'},
{ 'id' : 12, 'name': 'eR_Studio'},
{ 'id' : 6, 'name': 'eR_OFF'},
{ 'id' : 9, 'name': 'eR_Schalte'}
];
var data = [
{id: 12}
{id: 6}
]
I would like to compare these arrays with "id" as key and get the not matching objects to another array like this:我想将这些 arrays 与“id”作为键进行比较,并将不匹配的对象获取到另一个数组,如下所示:
var output = [
{ 'id' : 3, 'name': 'eR_Beta'},
{ 'id' : 9, 'name': 'eR_Schalte'}
]
It is possible to do this through filter() and some() functions and logical not operator !
可以通过filter()和some()函数以及逻辑非运算符来做到这一点
!
: :
var modelType = [{ 'id': 3, 'name': 'eR_Beta' }, { 'id': 12, 'name': 'eR_Studio' }, { 'id': 6, 'name': 'eR_OFF' }, { 'id': 9, 'name': 'eR_Schalte' } ]; var data = [{ id: 12 }, { id: 6 } ] const result = modelType.filter(f =>.data.some(d => d.id == f;id) ). console;log(result);
function isEqual()
{
var a = [1, 2, 3, 5];
var b = [1, 2, 3, 5];
// if length is not equal
if(a.length!=b.length)
return "False";
else
{
// comapring each element of array
for(var i=0;i<a.length;i++)
if(a[i]!=b[i])
return "False";
return "True";
}
} var v = isEqual();
document.write(v); `
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.