0

i am trying to filter the values of array one from array two. but i am not getting the three values. i only need the object with unique id.

i need { id: 28 ,name: "sdfsdf"} .. this only

i am trying this now . but getting there .any help ?

var arr1 = [
        {   
         id :  { oid : 23 },
         name : "nahid"
        } , 
        
         {  
         
         id :  { oid : 24 },
         name : "nahidsdsd"
    
        } , 
         {  
         
         id :  { oid : 26 },
         name : "nahidtytuyu"
    
        } , 
       
    ]
    
    var arr2 = [
         {   
         id :  { oid : 23 },
         name : "nahid"
        } , 
        
         {  
         
         id :  { oid : 24 },
         name : "nahidsdsd"
    
        } , 
         {  
         
         id :  { oid : 26 },
         name : "nahidtytuyu"
    
        } , 
         {  
         id :  { oid : 28 },
         name : "nahidtytuyu"
         } , 
    ]
    
    
    var res = arr2.filter( data => {
    
       var temp; 
       arr1.map( data2 =>  {
           temp = data2;
       } )
       
        return temp.id.oid != data.id.oid
     
        
    } )
    
    console.log(res)

(3) [{…}, {…}, {…}] 0 : {id: {…}, name: "nahid"} 1 : {id: {…}, name: "nahidsdsd"} 2 : {id: {…}, name: "nahidtytuyu"} length : 3

2
  • what you need as a result? didnt get your requiremtnt Commented Apr 10, 2018 at 12:34
  • i need { id: 28 ,name: "sdfsdf"} .. this only Commented Apr 10, 2018 at 12:35

3 Answers 3

1

Here is the required answer for you,

    var arr1 = [
        {   
         id :  { oid : 23 },
         name : "nahid"
        } , 
    
         {  
    
         id :  { oid : 24 },
         name : "nahidsdsd"
    
        } , 
         {  
    
         id :  { oid : 26 },
         name : "nahidtytuyu"
    
        } , 
    
    ]
    
    var arr2 = [
         {   
         id :  { oid : 23 },
         name : "nahid"
        } , 
    
         {  
    
         id :  { oid : 24 },
         name : "nahidsdsd"
    
        } , 
         {  
    
         id :  { oid : 26 },
         name : "nahidtytuyu"
    
        } , 
         {  
         id :  { oid : 28 },
         name : "nahidtytuyu"
         } , 
    ]
    
    function difference(otherArray){
      return function(current){
        return otherArray.filter(function(other){
          return other.id.oid == current.id.oid
        }).length == 0;
      }
    }
    
    var onlyInA = arr1.filter(difference(arr2));
    var onlyInB = arr2.filter(difference(arr1));
    
    result = onlyInA.concat(onlyInB);
    console.log(result)

Please run above code and check console

Here is a working demo

Sign up to request clarification or add additional context in comments.

Comments

0
var res = arr2.filter( data => {
var temp; 
var flag=1;

arr1.map( data2 =>  {
    temp = data2;
   if(temp.id.oid == data.id.oid)
   {
    flag= flag +1;
   }
});

if(flag == 1)
return data;
} )
console.log(res)
https://jsfiddle.net/dintal/xn2motsf/18/#&togetherjs=DEbQo04RUM

1 Comment

But this wont work, when the arrays are reversed. So it wont fulfill all use cases.
0

Edit: I think I get your use case now. Please check the code below

var arr1 = [
        {   
         id :  { oid : 23 },
         name : "nahid"
        } , 
        {  
         id :  { oid : 24 },
         name : "nahidsdsd"
        } , 
         {  
         id :  { oid : 26 },
         name : "nahidtytuyu"
        } , 
    ]
    var arr2 = [
         {   
         id :  { oid : 23 },
         name : "nahid"
        } ,   
         {  
         id :  { oid : 24 },
         name : "nahidsdsd"
        } , 
         {  
         id :  { oid : 26 },
         name : "nahidtytuyu"
        } , 
         {  
         id :  { oid : 28 },
         name : "nahidtytuyu"
         } , 
    ]
 var idMap = arr1.map(function(e) { return e.id.oid || null ; }) 
  var idMap2 = arr2.map(function(e) { return e.id.oid || null ; }) 
 var res = arr2.filter(function(data){
 return (idMap.indexOf(data.id.oid) == -1);   
}) ;
var res2 = arr1.filter(function(data){
 return (idMap2.indexOf(data.id.oid) == -1);   
})  
  res = res.concat(res2);
  console.log("result",res);

1 Comment

But this wont work, when the arrays are reversed. So it wont fulfill all use cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.