create map in javascript code example

Example 1: javascript map

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

Example 2: maps in javascript

// Map decalaration in javaScript
const obj1 = { name: 'ismail' };
const obj2 = { name: 'sulman' };
const obj3 = { name: 'naeem' };

firstMap = new Map([
    [
        [obj1, [{ date: 'yesterday', price: '10$' }]], // using object as a key in the map and object inside the array
        [obj2, [{ date: 'today', price: '100$' }]]
    ]
]);
firstMap.set(obj3, [{ date: "yesterday", price: '150$' }]); //pushing the obj to the Map

// iterating the Map
for (const entry of firstMap.entries()) {
    console.log(entry);
};

console.log(firstMap);

firstMap.delete(obj3);
console.log(firstMap);

Example 3: javascript map

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

listFruits()

// https://jsfiddle.net/tmoreland/16qfpkgb/3/

Example 4: map in javascript

['elem', 'another', 'name'].map((value, index, originalArray) => { 
  console.log(.....)
});

Example 5: how to create an element in js using the map method

const numbers = [1, 2, 3, 4, 5]; 

const bigNumbers = numbers.map(number => {
  return number * 10;
});