0

I have a problem to get the diffrent element from 2 array object. my example is just like this:

array1= [{id: 1, a: "a", b: "b"}, {id: 2, c: "c", d: "d"}, {id: 3, e: "e", f: "f"}];
array2 = [{c: "c", d: "d"}];

what I expected is:

the output Result is just like this:

this id in every object is not relevant.

result =[{a: "a", b: "b"},{e: "e", f: "f"}];

I have try to use filter or find in typescript, but is was not working.

any solutions??

2 Answers 2

2

Using lodash package and its isEqual method it may look like this:

const _ = require('lodash');

array1= [{id: 1, a: "a", b: "b"}, {id: 2, c: "c", d: "d"}, {id: 3, e: "e", f: "f"}];
array2 = [{c: "c", d: "d"}];

const results = array1.filter(item => !array2.some(item2 => _.isEqual(_.omit(item, ['id']), _.omit(item2, ['id']))))

That way you can compare objects with more that one level of props

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

1 Comment

Hi Anatoly, I have change something in my example
0

array1= [{a: "a", b: "b"}, {c: "c", d: "d"}, {e: "e", f: "f"}];
array2 = [{c: "c", d: "d"}];

const result = array1.filter(subary => {
  var flag = false;

  if (Object.keys(subary).length == Object.keys(array2[0]).length) {
    for (key in subary) {
      if (subary[key] == array2[0][key]) {
        continue;
      } else {
        flag = true;
        break;
      }
    }
  } else {
    flag = true;
  }
  return flag;
})

console.log(result);

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.