Syntax error Display resultant array based on the object’s order determined by the first array in JavaScript?

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' ]
Updated on: 2020-09-12T08:19:31+05:30

90 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements