I have two JSON arrays like this :
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 pare these arrays with "id" as key and get the not matching objects to another array like this :
var output = [
{ 'id' : 3, 'name': 'eR_Beta'},
{ 'id' : 9, 'name': 'eR_Schalte'}
]
I have two JSON arrays like this :
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 pare these arrays with "id" as key and get the not matching objects to another array like this :
var output = [
{ 'id' : 3, 'name': 'eR_Beta'},
{ 'id' : 9, 'name': 'eR_Schalte'}
]
Share
Improve this question
edited Jul 15, 2024 at 12:04
vsam
asked Oct 22, 2019 at 13:00
vsamvsam
6446 silver badges27 bronze badges
1
- Possible duplicate of (Compare two arrays of objects and remove items in the second one that have the same property value)[stackoverflow./questions/17830390/… – Mara Black Commented Oct 22, 2019 at 13:06
2 Answers
Reset to default 8It is possible to do this through filter() and some() functions and logical not operator !
:
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
{
// apring 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); `
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745256773a4619005.html
评论列表(0条)