
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript Match between 2 arrays
Let’s say, we have two arrays, one of String literals and another of objects.
const data = [{ name: 'Kamlesh Kapasi', uid: 123 }, { name: 'Mahesh Babu', uid: 129 }, { name: 'Akshay Kapoor', uid: 223 }, { name: 'Vikas Gupta', uid: 423 }, { name: 'Mohit Dalal', uid: 133 }, { name: 'Rajkumar Hirani', uid: 233 }, { name: 'Joy', uid: 127 }]; const names = ['Joy', 'Rajkumar Hirani', 'Akshay Kapoor', 'Mahesh Babu', 'Mohit Dalal', 'Kamlesh Kapasi', 'Vikas Gupta']
Our job is to write a function that iterates over the names array and constructs an array of Numbers that contains uid of specific names in the same order as they appear in the names array.
Let’s write the code for this function −
Example
const data = [{ name: 'Kamlesh Kapasi', uid: 123 }, { name: 'Mahesh Babu', uid: 129 }, { name: 'Akshay Kapoor', uid: 223 }, { name: 'Vikas Gupta', uid: 423 }, { name: 'Mohit Dalal', uid: 133 }, { name: 'Rajkumar Hirani', uid: 233 }, { name: 'Joy', uid: 127 }]; const names = ['Joy', 'Rajkumar Hirani', 'Akshay Kapoor', 'Mahesh Babu', 'Mohit Dalal', 'Kamlesh Kapasi', 'Vikas Gupta'] const mapId = (arr, names) => { return names.reduce((acc, val) => { const index = arr.findIndex(el => el.name === val); return acc.concat(arr[index].uid); }, []); } console.log(mapId(data, names));
Output
The output in the console will be −
[ 127, 233, 223, 129, 133, 123, 423 ]
- Related Articles
- Compare two arrays and get those values that did not match JavaScript
- Equality of two 2-D arrays - JavaScript
- Return the largest array between arrays JavaScript
- Finding the difference between two arrays - JavaScript
- JavaScript match()
- Find the Symmetric difference between two arrays - JavaScript
- How to combine 2 arrays into 1 object in JavaScript
- Column sum of elements of 2-D arrays in JavaScript
- How to get the difference between two arrays in JavaScript?
- Finding Common Item Between Arbitrary Number of Arrays in JavaScript
- Checking for the similarity of two 2-D arrays in JavaScript
- Javascript typed arrays
- JavaScript JSON Arrays
- Finding the missing number between two arrays of literals in JavaScript
- Building a Map from 2 arrays of values and keys in JavaScript

Advertisements