Set and Map javascript code example

Example 1: map object and add new property javascript

Results.map(obj=> ({ ...obj, Active: 'false' }))

Example 2: append to map javascript

var myMap = {};

myMap[newKey] = newValue;

// ...


// Another method

var myMap = new Map()
myMap.set("key0","value")
// ...
myMap.has("key1"); // evaluates to false, assuming key1 wasn't set

Example 3: map javascript

var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
  return num * 2;
});
// doubles is now [2, 8, 18]. numbers still [1, 4, 9]

Example 4: map in javascript

// Use map to create a new array in memory. Don't use if you're not returning
const arr = [1,2,3,4]

// Get squares of each element
const sqrs = arr.map((num) => num ** 2)
console.log(sqrs)
// [ 1, 4, 9, 16 ]

//Original array untouched
console.log(arr)
// [ 1, 2, 3, 4 ]