0

I would like to compare two array obj for a filter how to filter the array with objects strictly different from the objects of another array obj?

  var y = [

     {  nb :'rouge',
        xb :'rouge',
        x  : 12 },

       { nb :'red',
         xb :'rouge',
         x  : 12 }
     ]
     ,

  var   x = [

       {  nb :'rouge',
          xb : 'rouge',
           x : 12,
       },

       {  nb :'red',
          xb : 'rouge',
          x  : 12,
       },

       {  nb :'violet',
          xb : 'violet',
          x  : 12,
        },

]

  const dispo = x.filter( x !== y );
  console.log(dispo)

expected result :

[ { nb : violet, xb : 'violet', x : 12}

]

4
  • Can you explain the final purpose? Do you want to filter by object reference or by object properties? The object you wan extract can be anywhere in the array? Commented Apr 4, 2020 at 19:55
  • Why do you want an array with only 1 object inside would'nt it be better something like this: { nb : violet, xb : 'violet', x : 12}? Commented Apr 4, 2020 at 19:55
  • @jogarcia this is an example the objective is to be able to exclude in table 2 elements identical to the 1st table Commented Apr 4, 2020 at 20:04
  • @Lievno the final goal is to have an array which excludes elements identical to the other array: ! == xb &&! == nb (from table y) for example Commented Apr 4, 2020 at 20:06

2 Answers 2

1

You will need to compare the objects by value. You can use something like lodash's _.isEqual or _.isEqualDeep. A simple approach without lodash would be something like this:

function compare(a, b) 
{
  if (Object.keys(a).length !== Object.keys(b).length) {
    return false;
  }

  for (let p of Object.keys(a)) {
  
    if (!b.hasOwnProperty(p)) {
      return false;
    }
    
    if (a[p] !== b[p]) {
      return false
    }
    
  }
  
  return true;
}

function includesObj(array, obj) 
{
  for (let el of array) {
    if (compare(obj, el)) {
      return true;
    }
  }
  
  return false;
}  

console.log(x.filter(el => !includesObj(y, el)));

This will not do a deep comparison. It will only work if the values of your objects are primitives (not arrays, functions, other objects). But in your case it will also work.

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

Comments

0

You can't simply compare two objects in javascript because the references instead of the values of this objects are compared.

You can check this link to learn about that:

https://medium.com/javascript-in-plain-english/comparing-objects-in-javascript-ce2dc1f3de7f

If you prefer simplicity over profundity and the objects that you are putting inside de array are'nt to complex you could try Json.stringify() this transforms your object in a string making it easy to compare.

Notice that in the next example i changed the order of the array and the names for readibility.

var firstArray = [
  {
    nb: 'rouge',
    xb: 'rouge',
    x: 12,
  },

  {
    nb: 'red',
    xb: 'rouge',
    x: 12,
  },

  {
    nb: 'violet',
    xb: 'violet',
    x: 12,
  },
];

var secondArray = [
  {
    nb: 'rouge',
    xb: 'rouge',
    x: 12
  },

  {
    nb: 'red',
    xb: 'rouge',
    x: 12
  }
];

function difference(firstArray, secondArray) {
  return firstArray.filter(firstArrayElement => {
    return !isContainedInSecondArray(firstArrayElement, secondArray);
  });
}

function isContainedInSecondArray(firstArrayElement, secondArray) {
  return secondArray.find(secondArrayElement => {
    if (JSON.stringify(firstArrayElement) == JSON.stringify(secondArrayElement)) {
      return true;
    }
  });
}

//prints [ { nb: 'violet', xb: 'violet', x: 12 } ]
console.log(difference(firstArray, secondArray));
//prints []
console.log(difference(secondArray, firstArray));

Comments

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.