javascript new map code example

Example 1: map in javascript

// 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 ]

Example 2: new map js

let utilisateurs = new Map()

utilisateurs.set('Mark Zuckerberg' ,{
    email: '[email protected]',
    poste: 'PDG',
})

utilisateurs.set ('bill Gates',{
    email: '[email protected]' ,
    poste : 'sauver le monde' ,

})
    
console.log(utilisateurs);

Example 3: when to use map

When to use List, Set and Map?
If we need to access elements frequently by using index, then List is a way 
to go ArrayList provides faster access if we know index.
If we want to store elements and want them to maintain an order, 
then go for List again. List is an ordered collection and maintain order.
If we want to create collection of unique elements and don't want
any duplicate than choose any Set implementation. (HashSet... )
If we want store data in form Key and Value than Map is the way to go.
We can choose from HashMap, Hashtable...

Tags:

Cpp Example