2

I want to check an array has the object which given as parameter to js includes function. For example:

let arr = [
    {'name': 'max', 'age': '20'},
    {'name': 'max', 'age': '21'},
    {'name': 'jane', 'age': '22'}];
console.log(arr.includes({'name': 'max', 'age': '21'})); // FALSE

That give me "false" value. How can I check object equality in object array without for loop eq.

4 Answers 4

3

You can write a generic function like

let matches = flt => x => Object.keys(flt).every(k => flt[k] === x[k]);

and then

arr.some(matches({'name': 'max', 'age': '21'}))

This performs shallow, partial comparison. If you need other modes, like full and/or deep comparison, this would be far more tricky to implement. I'd suggest using lodash (_.isMatching, _.isEqual) in this case.

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

2 Comments

Huh. I like this one more than mine, well done. Is there any reason for using keys instead of entries? I mean, I'm pretty sure it would be the same, just wondering.
@briosheje: no particular reason, except that keys is slightly wider supported.
3

You cannot do this with includes because you don't handle the equality check and the objects are different, therefore, nothing matches.

To do what you want, you can use some or find !

let arr = [
    {'name': 'max', 'age': '20'},
    {'name': 'max', 'age': '21'},
    {'name': 'jane', 'age': '22'}
];

console.log(arr.some(({name, age}) => name === 'max' && age === '21'));
console.log(!!arr.find(({name, age}) => name === 'max' && age === '21'));

Please note that find returns the object, I've added !! to print a boolean result, but that's not necessarily mandatory.

2 Comments

If you know the structure of your object, it's keys to be exact then you can go ahead with sjahan's solution.
As some and find takes a function in parameter, you can easily implement any 'equality check function' that you want/need. Although, working not knowing what you handle/compare will likely have a perf cost. Having a 'universal' compare function that deeps into both objects is cool but could be expensive. If this does not answer your question, let me know and i'll delete it.
2

Here is an option relying on both .some and .entries to ensure that all the key-value pairs passed exists and are matched inside the targeted array. This also will ignore the order of the keys, and accepts a dynamic input.

This example assumes that if fewer keys are provided, only these are checked. For example, checking for {'name': 'max'} only will return true.

let arr = [
    {'name': 'max', 'age': '20'},
    {'name': 'max', 'age': '21'},
    {'name': 'jane', 'age': '22'}];

function includedInArray(lookups, _arr) {
  // Ensure every lookup match.
  return _arr.some(i => {
    // Loop key-value pairs.
    return Object.entries(lookups).every(([key, value]) => {
      return i[key] === value;
    });
  });
};

console.log(includedInArray({
  'name': 'max',
  'age': '21'
 }, arr)); // should return true.
 
 console.log(includedInArray({
  'age': '21',
  'name': 'max'
 }, arr)); // should return true.
 
 console.log(includedInArray({
  'age': '55',
  'name': 'max'
 }, arr)); // should return false.
 
 console.log(includedInArray({
  'name': 'max'
 }, arr)); // should return true.

Comments

2

Here's another way to achieve the same result :

If the structure of the object is not known well in before, i.e when they change dynamically, from an API or something then you can use the following solution

let arr = [
    {'name': 'max', 'age': '20'},
    {'name': 'max', 'age': '21'},
    {'name': 'jane', 'age': '22'}
];


let x =   {'name': 'max', 'age': '20'};
let result =arr.map( ele => JSON.stringify(ele)).includes(JSON.stringify(x));
console.log(result)

The above solution will work for nested objects as well.

Note The solution has two caveats

  1. It works provided that order of your keys remain constant throughout the array as well in the x object, the one which you wish to compare
  2. If there is too much of nesting in your objects then it will be really slow. I won't recommend JSON.stringify for anyone who care about performance.

Edit 1

  1. Doesn't work with Circular structures

1 Comment

You can add: 3. Doesn't work with Circular structures`

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.