var arr = [
{ key: 'foo', val: 'bar' },
{ key: 'hello', val: 'world' }
];
var result = new Map(arr.map(i => [i.key, i.val]));
// When using TypeScript, need to specify type:
// var result = arr.map((i): [string, string] => [i.key, i.val])
// Unfortunately maps don't stringify well. This is the contents in array form.
console.log("Result is: " + JSON.stringify([...result]));
// Map {"foo" => "bar", "hello" => "world"}
Run code snippet
let numbers = [1, 2, 3, 4]
let filteredNumbers = numbers.map(function(_, index) {
if (index < 3) {
return num
}
})
// index goes from 0, so the filterNumbers are 1,2,3 and undefined.
// filteredNumbers is [1, 2, 3, undefined]
// numbers is still [1, 2, 3, 4]
// make new array from edited items of another array
var newArray = unchangedArray.map(function(item, index){
return item; // do something to returned item
});
// same in ES6 style (IE not supported)
var newArray2 = unchangedArray.map((item, index) => item);
let arr = [1,2,3]
/*
map accepts a callback function, and each value of arr is passed to the
callback function. You define the callback function as you would a regular
function, you're just doing it inside the map function
map applies the code in the callback function to each value of arr,
and creates a new array based on your callback functions return values
*/
let mappedArr = arr.map(function(value){
return value + 1
})
// mappedArr is:
> [2,3,4]
function square(arr) {
const newArr = arr.map(x => x * x );
return newArr ;
//if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
const result = new Map();//create a new map
result.set('a', 1);
result.set('b', 2);
console.log(result);
const germany = {name: 'Germany' , population: 8000000};//create a new object
result.set(germany, '80m');//set the object to the map
console.log(result);
result.delete('a');//delete the key 'a'
console.log(result);
result.clear();//clear all the keys in the map
console.log(result);
let numbers = [1, 2, 3, 4]
let filteredNumbers = numbers.map(function(num, index) {
if (index < 3) {
return num
}
})
// index goes from 0, so the filterNumbers are 1,2,3 and undefined.
// filteredNumbers is [1, 2, 3, undefined]
// numbers is still [1, 2, 3, 4]