0

I want to compare two arrays with objects with different key names using underscore...

array1 = [{email:"[email protected]", first_name:"asad"}, {email:"[email protected]", first_name:"name1"}]

array2 = [{email2:"[email protected]", first_name2:"asad"}, {email2:"[email protected]", first_name2:"name22"}]

Want Output of unique email IDS from array2 which is NOT present in array1 like this...

array3 = [{email2:"[email protected]", first_name2:"name22"}]

1 Answer 1

2

Convert array1 to an object with the email addresses as keys, then filter array2 based on that:

var t = {};
angular.forEach(array1, function(v) { t[v.email] = 1 });
var array3 = array2.filter(
    function (v) {
       return angular.isUndefined(t[v.email2]);
    });

Sorry, underscorejs not needed here.

Or if you can use modern JS syntax it reads a bit more cleanly:

var t = {};
array1.forEach(v => t[v.email] = 1);
var array3 = array2.filter(v => angular.isUndefined(t[v.email2]));
Sign up to request clarification or add additional context in comments.

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.