lewkir
0
Q:

nodejs map

const myArray = ['Sam', 'Alice', 'Nick', 'Matt'];

// Appends text to each element of the array
const newArray = myArray.map(name => {
	return 'My name is ' + name; 
});
console.log(newArray); // ['My name is Sam', 'My Name is Alice', ...]

// Appends the index of each element with it's value
const anotherArray = myArray.map((value, index) => index + ": " + value);
console.log(anotherArray); // ['0: Sam', '1: Alice', '2: Nick', ...]

// Starting array is unchanged
console.log(myArray); // ['Sam', 'Alice', 'Nick', 'Matt']
9
array.map((item) => {
  return item * 2
} // an example that will map through a a list of items and return a new array with the item multiplied by 2
21
const numbers = [0,1,2,3];

console.log(numbers.map((number) => {
  return number;
}));
2
function listFruits() {
  let fruits = ["apple", "cherry", "pear"]
  
  fruits.map((fruit, index) => {
    console.log(index, fruit)
  })
}

listFruits()

// https://jsfiddle.net/tmoreland/16qfpkgb/3/
10
let wrongMap = new Map()
wrongMap['bla'] = 'blaa'
wrongMap['bla2'] = 'blaaa2'

console.log(wrongMap)  // Map { bla: 'blaa', bla2: 'blaaa2' }
1
var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
  return num * 2;
});
// doubles is now [2, 8, 18]. numbers still [1, 4, 9]
0
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
4
array.map( item => item.id )

array2.map( item => item.toString() )

array2.map( item => item * 3 )

// this will apply a transformation to all elements of an array
// "" item => something "" is the transformation (function)
0

New to Communities?

Join the community