

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to compare two arrays when the key is a string - JavaScript
Let’s say the following is our first array −
const firstArray = [ { "name": "John Doe" }, { "name": "John Smith" }, { "name": "David Miller" }, { "name": "Bob Taylor" }, { "name": "Carol taylor" }, { "name": "Adam Smith" }, ];
Let’s say the following is our second array −
const secondArray = [ { "name": "Adam Smith" }, { "name": "John Doe" }, { "name": "David Miller" }, { "name": "James Taylor" } ];
To compare, use map() and includes().
Example
Following is the code −
const firstArray = [ { "name": "John Doe" }, { "name": "John Smith" }, { "name": "David Miller" }, { "name": "Bob Taylor" }, { "name": "Carol taylor" }, { "name": "Adam Smith" }, ]; const secondArray = [ { "name": "Adam Smith" }, { "name": "John Doe" }, { "name": "David Miller" }, { "name": "James Taylor" } ]; const getAllValue = ({ 'name': name }) => name; const result = firstArray .map(getAllValue) .filter(value => secondArray .map(getAllValue) .includes(value) ); console.log(result);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo251.js
Output
This will produce the following output on console −
PS C:\Users\Amit\javascript-code> node demo251.js [ 'John Doe', 'David Miller', 'Adam Smith' ]
- Related Questions & Answers
- How to compare two string arrays, case insensitive and independent about ordering JavaScript, ES6
- How to compare two arrays in C#?
- How to compare two arrays in Java?
- How to compare arrays in JavaScript?
- Compare Two Java long Arrays
- Compare Two Java double arrays
- Compare two arrays of single characters and return the difference? JavaScript
- Java Program to compare two Byte Arrays
- Java Program to compare two Boolean Arrays
- How to Compare two String in Java?
- How do we compare two arrays in Java?
- How to compare two arrays in JavaScript and make a new one of true and false? JavaScript
- Compare and fill arrays - JavaScript
- How to compare two arrays to see how many same elements they have in JavaScript?
- Java Program to compare two Java char Arrays
Advertisements