Skip to content

Instantly share code, notes, and snippets.

@timgarciaa
Last active July 8, 2022 14:42
Show Gist options
  • Save timgarciaa/49118a86447a1332dd69653e890c50a5 to your computer and use it in GitHub Desktop.
Save timgarciaa/49118a86447a1332dd69653e890c50a5 to your computer and use it in GitHub Desktop.
How to use map function of array.
var fruits = ['apple', 'mango', 'orange', 'grapes'];
fruits.map(fruit => console.log(fruit));
// apple
// mango
// orange
// grapes
var fruitJuice = fruits.map(fruit => fruit + ' juice');
console.log(fruitJuice);
//[ 'apple juice', 'mango juice', 'orange juice', 'grapes juice' ]
var num = 1;
var fruitObject = fruits.map(fruit => {
// do your logic here
var numberedFruit = {};
numberedFruit.number = num++;
numberedFruit.fruit = fruit;
return numberedFruit;
});
console.log(fruitObject);
// [
// { number: 1, fruit: "apple" },
// { number: 2, fruit: "mango" },
// { number: 3, fruit: "orange" },
// { number: 4, fruit: "grapes" },
// ];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment