3

New to JavaScript and have very limited knowledge of JS objects. I want to get difference of two JS objects.

a=[{"month":1,"year":2015},{"month":2,"year":2015},{"month":3,"year":2015},{"month":4,"year":2015},{"month":5,"year":2015}];
b=[{"month":1,"year":2015},{"month":2,"year":2015},{"month":5,"year":2015}];

The result I dreamed should be

result=[{"month":3,"year":2015},{"month":4,"year":2015}];

I got the above format using JSON.stringify.

Teemu
  • 22,918
  • 7
  • 53
  • 106
LetMeCodeYou
  • 320
  • 1
  • 9
  • possible duplicate of [Simplest code for array intersection in javascript](http://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript) – Davin Tryon Jan 30 '15 at 11:29
  • possible duplicate of [Difference in JSON objects using Javascript/JQuery](http://stackoverflow.com/questions/1200562/difference-in-json-objects-using-javascript-jquery) – demo Jan 30 '15 at 11:29
  • what if `b` has something `a` doesn't have!! by difference did you also consider that scenario! – Md Ashaduzzaman Jan 30 '15 at 11:31
  • 1
    @demo I tried several solutions available on SO. No one really worked for me or too complex for a simple problem. Otherwise I'm aware of the worth of time. – LetMeCodeYou Jan 30 '15 at 11:31
  • @AshadShanto possibly in this particular case it will not happen. I'm tracking this thing already. – LetMeCodeYou Jan 30 '15 at 11:33

1 Answers1

4

Below should work for you,just use filter on array.

var a=[{"month":1,"year":2015},{"month":2,"year":2015},{"month":3,"year":2015},{"month":4,"year":2015},{"month":5,"year":2015}];
var b=[{"month":1,"year":2015},{"month":2,"year":2015},{"month":5,"year":2015}];

var diff = a.filter(function(a){
    return b.filter(function(b){
        return b.month == a.month && b.year == a.year
    }).length == 0
});

console.log(diff);
dReAmEr
  • 6,986
  • 7
  • 36
  • 63
  • 1
    In my code I'm getting `undefined is not a function` How can I make it into a regular function form? so that I can declare it above the arrays where data is actually populating in them. – LetMeCodeYou Jan 30 '15 at 12:39
  • @LetMeCodeYou unable to understand ,could you please elaborate more? or show us your code? – dReAmEr Jan 30 '15 at 12:48
  • 1
    Here we go, [fiddle](http://jsfiddle.net/LetMeCodeYou/1tyossmx/) It is just to show the structure of code where I'm using your function. Please suggest how I can overcome this `uncaught type error:undefined is not a function` – LetMeCodeYou Jan 30 '15 at 13:03
  • The problem is ,you are trying to use JSON on filter method,filter method only works with Object,I have updated your code at fiddle,just check and let me know – dReAmEr Jan 30 '15 at 13:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69891/discussion-between-re350-and-letmecodeyou). – dReAmEr Jan 30 '15 at 13:17
  • 1
    Actually I don't need the `JSON.stringify` at all. I removed it and error is gone. But the results are looks like this `differenceOfMonthYearData: monthYearDataToBeMarked: , monthYearDataToBeFiltered: ,,,` empty arrays. commas shows that there is some data. But it is not showing. – LetMeCodeYou Jan 30 '15 at 13:18