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

JavascriptWeb DevelopmentObject Oriented Programming

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' ]
raja
Published on 12-Sep-2020 08:18:59
Advertisements