1array.map((item) => {
2 return item * 2
3} // an example that will map through a a list of items and return a new array with the item multiplied by 2
1// Map decalaration in javaScript
2const obj1 = { name: 'ismail' };
3const obj2 = { name: 'sulman' };
4const obj3 = { name: 'naeem' };
5
6firstMap = new Map([
7 [
8 [obj1, [{ date: 'yesterday', price: '10$' }]], // using object as a key in the map and object inside the array
9 [obj2, [{ date: 'today', price: '100$' }]]
10 ]
11]);
12firstMap.set(obj3, [{ date: "yesterday", price: '150$' }]); //pushing the obj to the Map
13
14// iterating the Map
15for (const entry of firstMap.entries()) {
16 console.log(entry);
17};
18
19console.log(firstMap);
20
21firstMap.delete(obj3);
22console.log(firstMap);
1let map = new Map()
2map['bla'] = 'blaa'
3map['bla2'] = 'blaaa2'
4
5console.log(map) // Map { bla: 'blaa', bla2: 'blaaa2' }
6
1// Use map to create a new array in memory. Don't use if you're not returning
2const arr = [1,2,3,4]
3
4// Get squares of each element
5const sqrs = arr.map((num) => num ** 2)
6console.log(sqrs)
7// [ 1, 4, 9, 16 ]
8
9//Original array untouched
10console.log(arr)
11// [ 1, 2, 3, 4 ]
1//map() methods returns a new array
2const data = {name: "laptop", brands: ["dell", "acer", "asus"]}
3let inside_data = data.brands.map((i) => {
4 console.log(i); //dell acer asus
5});
1let myMap = new Map()
2
3let keyString = 'a string'
4let keyObj = {}
5// setting the values
6myMap.set(keyString, "value associated with 'a string'")
7myMap.set(keyObj, 'value associated with keyObj')
8myMap.set(keyFunc, 'value associated with keyFunc')
9myMap.size // 3
10// getting the values
11myMap.get(keyString) // "value associated with 'a string'"
12myMap.get(keyObj) // "value associated with keyObj"
13myMap.get(keyFunc) // "value associated with keyFunc"