- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Display resultant array based on the object’s order determined by the first array in JavaScript?
Let’s say the following is our object −
var lastName ={ "John":"Smith", "David":"Miller", "Bob":"Taylor" }
Following is our array −
var firstName=[ "Bob", "John", "David" ]
Display resultant array based on the object’s order determined by the first array, use map(). Following is the code −
Example
var firstName=[ "Bob", "John", "David" ] var lastName ={ "John":"Smith", "David":"Miller", "Bob":"Taylor" } var values = firstName.map(getValues => lastName[getValues]); console.log(values);
To run the above program, you need to use the following command −
node fileName.js.
Output
Here, my file name is demo168.js. This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo168.js [ 'Taylor', 'Smith', 'Miller' ]
Advertisements