• Home
  • Jobs
  • Courses
  • Questions
  • Teachers
  • For business
  • ES/EN

0

31
Views
How to compare objects of two arrays in JavaScript

I had taken two arrays in JavaScript

arr1 = ["empid","Name"];

arr2 = [{"keyName":"empid" ,"keyValue":"2"}]

And I want to check the value of keyName should be any one element from arr1.

about 2 months ago ·

Juan Pablo Isaza

2 answers
Answer question

0

some short-circuits after finding the first match so it doesn't necessarily have to iterate over the whole array of objects. And it also returns a boolean which satisfies your use-case.

const query1 = ['empid','Name'];
const arr1 = [{'keyName':'empid' ,'keyValue':'2'}];
const query2 = ['empid','Name'];
const arr2 = [{'keyName':'empid2' ,'keyValue':'five'}];
const query3 = ['empid','Name', 'test'];
const arr3 = [{'keyName':'test2' ,'keyValue':'five'},{'keyName':'test' ,'keyValue':'five'}];

function found(arr, query) {
  return arr.some(obj => {
    return query.includes(obj.keyName);
  });
}

console.log(found(arr1, query1));
console.log(found(arr2, query2));
console.log(found(arr3, query3));

about 2 months ago · Juan Pablo Isaza Report

0

Use _.isEqual(object, other); It may help you.

about 2 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.