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]
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 ( ͡~ ͜ʖ ͡°)
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]