How to get not matching objects from two JSON array in Javascript - Stack Overflow

I have two JSON arrays like this :var modelType = [{ 'id' : 3,'name': 'eR_

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
Add a ment  | 

2 Answers 2

Reset to default 8

It 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信