0

There is a similar question on StackOverflow here in this question we need to get the difference in the array.

But I just want true or false. I don't need the different array.

I tried it like this:

groups = [{id: 1, name: "check 1"}, {id: 2, name: "check 2"}]

checkedgroups = [{id: 1, name: "check 1"}, {id: 2, name: "check 2"}, {id: 3, name: "check 3"}]

My solution: _.differenceBy(groups, checkedGroups, 'id').length

9
  • 3
    What is your differenceBy function? Commented Jan 24, 2019 at 6:37
  • Please post your differenceBy function. I suspect the error is caused by something in there. Commented Jan 24, 2019 at 6:37
  • 1
    If you need length difference then why no checkedgroups.length - groups.length Commented Jan 24, 2019 at 6:38
  • @brk i need to check the difference by considering id as common between these two aray. Commented Jan 24, 2019 at 6:39
  • 1
    You say you want a true or false as your output, but then at the bottom of your question, you say you expect a number representing the length as the output? Which one is it? Commented Jan 24, 2019 at 6:43

6 Answers 6

1

You can try different arrays and check the output. It uses a filter function. If the elements are not equal the array returned by filter is empty. Otherwise array of length of the original array is returned.

var a = ['a', 'b'];
var b = ['a', 'b'];

function w(a, b) {
  if (a.length != b.length)
    return false;
  else {
    var k = a.filter((e) => b[a.indexOf(e)] == e)
    if (k.length == 0)
      return false;
    else if (k.length == a.length)
      return true
  }
}
console.log(w(a, b))

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

Comments

0

You can use isEqualWith as follows:

_.isEqualWith(array, other, (objValue, othValue) => {
  if(objValue.id === otherValue.id && objValue.name === otherValue.name){
   return true'
  }
})

Comments

0

If it's just a JSON string

try{
    return JSON.stringify(groups) === JSON.stringify(checkedgroups);
} catch(e) {
    return false
}

Comments

0

If you want to get the number of items which differ from one array to another you can map both your objects to an array of id's (or whatever you set the prop argument to). You can then use .filter to get the difference of arr1 - arr2. Lastly, you can then return the .length of the difference between the two arrays.

See working example below:

const groups = [{id: 1, name: "check 1"}, {id: 2, name: "check 2"}],
checkedgroups = [{id: 1, name: "check 1"}, {id: 2, name: "check 2"}, {id: 3, name: "check 3"}];

const differenceBy = (arr1, arr2, prop) => {
  const arrProp1 = arr1.map((obj) => obj[prop]),
  arrProp2 = arr2.map((obj) => obj[prop]),
  
  dif = arrProp1.filter(num => !arrProp2.includes(num));
  return dif.length;
}

console.log(differenceBy(checkedgroups, groups, "id"));

Comments

0

To compare any objects with each other (which have no circular reference) use these:

If you're sure whether they ain't null and undefined:
JSON.stringify(groups) === JSON.stringify(checkedgroups)

Otherwise:
JSON.stringify(groups || null) === JSON.stringify(checkedgroups || null)

Comments

0

You can get the arrays of id's to compare the string version of the arrays:

var groups = [{id: 1, name: "check 1"}, {id: 2, name: "check 2"}]

var checkedgroups = [{id: 1, name: "check 1"}, {id: 2, name: "check 2"}, {id: 3, name: "check 3"}]

function differenceBy(g, c, id){
  var id1 = JSON.stringify(g.map(i => i[id]));
  var id2 = JSON.stringify(c.map(i => i[id]));
  if(id1 == id2) return true;
  else return false;
}


console.log(differenceBy(groups, checkedgroups, 'id'))

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.