How to get the difference between two arrays of objects in JavaScript

前端 未结 18 1363
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 04:45

I have two result sets like this:

// Result 1
[
    { value: \"0\", display: \"Jamsheer\" },
    { value: \"1\", display: \"Muhammed\" },
    { value: \"2\",         


        
18条回答
  •  故里飘歌
    2020-11-22 05:18

    let obj1 =[
                     { id: 1, submenu_name: 'login' },
                     { id: 2, submenu_name: 'Profile',}, 
                     { id: 3, submenu_name: 'password',  },  
                     { id: 4, submenu_name: 'reset',}
                   ] ;
     let obj2 =[
                     { id: 2}, 
                     { id: 3 },
                   ] ;
                   
    // Need Similar obj 
    const result1 = obj1.filter(function(o1){
     return obj2.some(function(o2){
        return o1.id == o2.id;          // id is unnique both array object
      });
    });
     console.log(result1);
    
    
    
    // Need differnt obj 
     const result2 = obj1.filter(function(o1){
     return !obj2.some(function(o2){    //  for diffrent we use NOT (!) befor obj2 here
        return o1.id == o2.id;          // id is unnique both array object
      });
    });
     console.log(result2);

提交回复
热议问题