- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compare array of objects - JavaScript
We have two arrays of objects like these −
const blocks = [ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, ] const containers = [ { block: { id: 1 } }, { block: { id: 2 } }, { block: { id: 3 } }, ]
We are required to write a function that checks each object of blocks array with the block key of each object of containers array and see if there exists any id in blocks array that is not present in the containers array. If so, we return false, otherwise we return true.
Example
Let’s write the code −
const blocks = [ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, ] const containers = [ { block: { id: 1 } }, { block: { id: 2 } }, { block: { id: 3 } }, ] const checkProperty = (first, second) => { const findInContainers = id => { for(let i = 0; i < second.length; i++){ if(second[i].block.id === id){ return true; }; }; return false; }; for(let i = 0; i < first.length; i++){ if(!findInContainers(first[i].id)){ return false; }; }; return true; }; console.log(checkProperty(blocks, containers));
Output
Following is the output in the console −
false
- Related Articles
- How to compare two JavaScript array objects using jQuery/JavaScript?
- How to compare two JavaScript Date Objects?
- How to compare two objects in JavaScript?
- JavaScript - length of array objects
- Manipulating objects in array of objects in JavaScript
- Creating an array of objects based on another array of objects JavaScript
- How to compare attributes of different objects in MongoDB object array?
- Update array of objects with JavaScript?
- Filtering array of objects in JavaScript
- Combine array of objects in JavaScript
- Filter JavaScript array of objects with another array
- Using methods of array on array of JavaScript objects?
- Array of objects to array of arrays in JavaScript
- Converting array of objects to an object of objects in JavaScript
- Search from an array of objects via array of string to get array of objects in JavaScript

Advertisements