0

enter image description hereI have two array of objects in javascript

var arr1 = [{'a':'1'},{'b':'2'},{'c':'3'}];
var arr2 = [{'a':'1'},{'b':'2'},{'d':'4'}];

I want to get the element from arr2 which is not in the arr1. my output will be [{'d':'4'}]

6
  • 1
    Possible duplicate of JavaScript array difference Commented Jul 16, 2016 at 18:31
  • is this work for array of objects? Commented Jul 16, 2016 at 18:35
  • 1
    None of the elements in arr2 are in arr1, they are all different? Commented Jul 16, 2016 at 18:43
  • you can use loops, filter, reduce.. there are several ways, what have you tried so far? Commented Jul 16, 2016 at 18:45
  • 2
    Define "element is in array". All the objects in your question have different references, so JS considers them different. Commented Jul 16, 2016 at 19:02

3 Answers 3

1

The easiest method that came my mind is using JSON.stringify:

var arr1 = [{'a':'1'},{'b':'2'},{'c':'3'}];
var arr2 = [{'a':'1'},{'b':'2'},{'d':'4'}];

stringArr1 = JSON.stringify(arr1);
var result = arr2.filter(
  obj => !~stringArr1.indexOf(JSON.stringify(obj))
);

console.log(result);

But there should be better ways.


The equivalent of:

var result = arr2.filter(
  obj => !~stringArr1.indexOf(JSON.stringify(obj))
);

is the common:

var result = arr2.filter(function (obj) {
  var stringObj = JSON.stringify(obj);
  if (stringArr1.indexOf(stringObj) != -1)
    return true;
  else
    return false;
});

The tricks are simple, basically you need to know 3 things:

  1. All non-zero numbers are true. SO link.
  2. The bitwise not (~) turns -1 into 0. MDN link.
  3. Inline arrow functions (=>) does not need return keyword. MDN link.

Hope it helps :)

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

1 Comment

If u dont mind can u explain obj => !~stringArr1.indexOf(JSON.stringify(obj)) this line and tell me the use of "~".
0

You could use JSON.stringify for each element of the arrays and use a hash table for checking

var arr1 = [{ 'a': '1' }, { 'b': '2' }, { 'c': '3' }],
    arr2 = [{ 'a': '1' }, { 'b': '2' }, { 'd': '4' }],
    arr3,
    hash = Object.create(null);

arr1.forEach(function (o) {
    hash[JSON.stringify(o)] = true;
});

arr3 = arr2.filter(function (o) {
    return !hash[JSON.stringify(o)];
});

console.log(arr3);

Comments

0

You can use $.grep(), .every(), Object.keys()

var arr1 = [{'a':'1'},{'b':'2'},{'c':'3'}];
var arr2 = [{'a':'1'},{'b':'2'},{'d':'4'}];
var res = $.grep(arr2, function(v) {
             return arr1.every(function(_v) {
               var keys = [Object.keys(v)[0], Object.keys(_v)[0]];
               return keys[0] !== keys[1] && v[keys[0]] !== _v[keys[1]]
             })
          });
console.log(res)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>

8 Comments

if i use other objects ,Uncaught TypeError: arr1.every is not a function is comming
"if i use other objects" What do you mean by "other objects"? Can you create a jsfiddle jsfiddle.net to demonstrate?
i use other data instead of "arr1" and "arr2" data
What is "other data"? Can you fork jsfiddle jsfiddle.net/1neqy2e5 to demonstrate? Include "other data" at Question?
@GowthamV. every is a function of Array.prototype, you can't use it with other data.
|

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.