简体   繁体   中英

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.

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));

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM