Q:

map in javascript

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
// Use map to create a new array in memory. Don't use if you're not returning
const arr = [1,2,3,4]

// Get squares of each element
const sqrs = arr.map((num) => num ** 2)
console.log(sqrs)
// [ 1, 4, 9, 16 ]

//Original array untouched
console.log(arr)
// [ 1, 2, 3, 4 ]
3
const data = {name: "laptop", brands: ["dell", "acer", "asus"]}
let inside_data = data.brands.map((i) => {
	console.log(i);
});
5
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
// map1 contains  
// 1 => 2 
// 2 => 3 
// 4 -> 5 
var map1 = new Map([[1 , 2], [2 ,3 ] ,[4, 5]]); 
  
console.log("Map1"); 
console.log(map1); 
  
// map2 contains  
// firstname => sumit 
// lastname => ghosh 
// website => geeksforgeeks  
var map2 = new Map([["firstname" ,"sumit"],  
        ["lastname", "ghosh"], ["website", "geeksforgeeks"]]); 
  
console.log("Map2"); 
console.log(map2); 
  
  
// map3 contains 
// Whole number => [1, 2, 3, 4] 
// Decimal number => [1.1, 1.2, 1.3, 1.4] 
// Negative number => [-1, -2, -3, -4] 
var map3 = new Map([["whole numbers", [1 ,2 ,3 ,4]], 
            ["Decimal numbers" , [1.1, 1.2, 1.3, 1.4]], 
            ["negative numbers", [-1, -2, -3, -4]]]); 
  
console.log("Map3"); 
console.log(map3); 
  
  
// map 4 contains  
// storing arrays both as key and value 
// "first name ", "Last name" => "sumit", "ghosh" 
// "friend 1", "sourav" => "friend 2", "gourav" 
var map4 = new Map([[["first name", "last name"], 
            ["sumit", "ghosh"]], 
            [["friend 1", "friend 2"], 
            ["sourav","gourav"]]]); 
  
console.log("Map4"); 
console.log(map4); 
1
let myMap = new Map()

let keyString = 'a string'
let keyObj    = {}
// setting the values
myMap.set(keyString, "value associated with 'a string'")
myMap.set(keyObj, 'value associated with keyObj')
myMap.set(keyFunc, 'value associated with keyFunc')
myMap.size              // 3
// getting the values
myMap.get(keyString)    // "value associated with 'a string'"
myMap.get(keyObj)       // "value associated with keyObj"
myMap.get(keyFunc)      // "value associated with keyFunc"
3

array.map(function(currentValue, index, arr), thisValue)
7
const arr = [0, 1, 2];

arr.map((number) => {
  console.log(number);
});
1

New to Communities?

Join the community