0

I have 2 arrays like below:

a = [
    {id:item, m:1, n:11, o:111},
    {id:item, m:2, n:21, o:211},
    {id:item, m:3, n:31, o:311},
    {id:'group', items:[
        {id:item, m:4, n:41, o:411},
        {id:item, m:5, n:51, o:511}]
    }
];

b = [
    {id:item, m:1, n:11, o:111, p:101},
    {id:item, m:2, n:21, o:211, p:121},
    {id:item, m:3, n:31, o:311, p:131},
    {id:'group', items:[
        {id:item, m:4, n:41, o:411, p:141},
        {id:item, m:5, n:51, o:511, p:151}]
    }
];

I need to compare these arrays for equality with only m,n,o columns. How to do this in optimal way? The output expected here is true, because those fields have the same values.

var a = [
    {id:'item', m:1, n:11, o:111},
    {id:'item', m:2, n:21, o:211},
    {id:'item', m:3, n:31, o:311},
    {id:'group', items:[{id:'item', m:4, n:41, o:411},
    {id:'item', m:5, n:51, o:511},
]}
];

var b = [
    {id:'item', m:1, n:11, o:111, p:101},
    {id:'item', m:2, n:21, o:211, p:121},
    {id:'item', m:3, n:31, o:311, p:131},
    {id:'group', items:[{id:'item', m:4, n:41, o:411, p:141},
    {id:'item', m:5, n:51, o:511, p:151},
]}
];

var output = _.isEqual(a,b);
$('.res').text(output);
<div class='res'>Arrays Equal?</div>

Any help is greatly appreciated.

1
  • 3
    It's not clear what you want as a result. Do you want to know if all the elements in the two arrays are the same using those columns? Or just want to know which elements are in common? How are the nested objects used, does it have to operate recursively? Commented Jan 30, 2015 at 21:28

2 Answers 2

1

you can do it like this (for m comparison):

a[0].m == b[0].m
Sign up to request clarification or add additional context in comments.

Comments

0

You can iterate through the arrays, comparing each object's m, n, and o values.

The difficulty comes with the nested items, which also have m, n, and o properties. In that case, you can call the function recursively on a[i].items and b[i].items.

function equals(a,b) {
  for(var i = 0 ; i < a.length ; i++) {
    if(a[i].items) {
      if(!equals(a[i].items, b[i].items)) {
        return false;
      }
    }
    else if(a[i].m !== b[i].m || a[i].n !== b[i].n || a[i].o !== b[i].o) {
      return false;
    }
  }
  return true;
} //equals

var a = [
    {id:'item', m:1, n:11, o:111},
    {id:'item', m:2, n:21, o:211},
    {id:'item', m:3, n:31, o:311},
    {id:'group', items:[
      {id:'item', m:4, n:41, o:411},
      {id:'item', m:5, n:51, o:511}
    ]}
];

var b = [
    {id:'item', m:1, n:11, o:111, p:101},
    {id:'item', m:2, n:21, o:211, p:121},
    {id:'item', m:3, n:31, o:311, p:131},
    {id:'group', items:[
      {id:'item', m:4, n:41, o:411, p:141},
      {id:'item', m:5, n:51, o:511, p:151}
    ]}
];

alert(equals(a,b));

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.