Quick .map example
This is meant to serve as a very quick introduction/reminder of how map works in javascript. I am a big fan of quick, simple examples.
I’m going to assume that you have node.js running. If you don’t, please install it and come back. https://nodejs.org/en/
In a command line session, start a node session by simply typing node
We’re going to start by creating some test data.
var officers = [
{ id: 18, name: ‘Captain Smith’ },
{ id: 20, name: ‘Major Thompson’ },
{ id: 32, name: ‘Lt Dan’ },
{ id: 44, name: ‘General Johnson’ },
];
You can key or paste the officer data in your node session.
You should then be able to see the officers data if you console.log the variable.
The map function over this data is really quite simple.
Get Scott Zuke’s stories in your inbox
Join Medium for free to get updates from this writer.
officers.map(function (officer) { return officer.id });
The map simply returns the data item that you specify, in this case, the the id.
Likewise, you can change out the specified field and return the officer name.
This is meant to be just as simple as it seems. I hope you are able to make use of the map function in your endeavors.







