maps in js code example
Example 1: javascript map array
const myArray = ['Sam', 'Alice', 'Nick', 'Matt'];
const newArray = myArray.map(name => {
return 'My name is ' + name;
});
console.log(newArray);
const anotherArray = myArray.map((value, index) => index + ": " + value);
console.log(anotherArray);
console.log(myArray);
Example 2: javascript map
array.map((item) => {
return item * 2
}
Example 3: maps in javascript
const obj1 = { name: 'ismail' };
const obj2 = { name: 'sulman' };
const obj3 = { name: 'naeem' };
firstMap = new Map([
[
[obj1, [{ date: 'yesterday', price: '10$' }]],
[obj2, [{ date: 'today', price: '100$' }]]
]
]);
firstMap.set(obj3, [{ date: "yesterday", price: '150$' }]);
for (const entry of firstMap.entries()) {
console.log(entry);
};
console.log(firstMap);
firstMap.delete(obj3);
console.log(firstMap);
Example 4: how the map function works javascript
const array = [2, 5, 9];
let squares = array.map((num) => num * num);
console.log(array);
console.log(squares);
Example 5: map in javascript
const arr = [1,2,3,4]
const sqrs = arr.map((num) => num ** 2)
console.log(sqrs)
console.log(arr)
Example 6: map in js
const data = {name: "laptop", brands: ["dell", "acer", "asus"]}
let inside_data = data.brands.map((i) => {
console.log(i);
});