maps javascript code example
Example 1: javascript map
array.map((item) => {
return item * 2
}
Example 2: 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 3: javascript map
function listFruits() {
let fruits = ["apple", "cherry", "pear"]
fruits.map((fruit, index) => {
console.log(index, fruit)
})
}
listFruits()
Example 4: map javascript
var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
return num * 2;
});
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 javascript
function map(collection, action) {
let result = [];
if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
result.push(action(collection[i], i, collection));
}
}
else {
for (let key in collection) {
result.push(action(collection[key], key, collection));
}
} return result;
}