map to create new object javascript code example
Example 1: object to map javascript
const map = new Map(Object.entries({foo: 'bar'}));
map.get('foo');
Example 2: typescript map list to new list of objects
var result = arr.map(person => ({ value: person.id, text: person.name }));
console.log(result)
Example 3: map through keys javascript
var myObject = { 'a': 1, 'b': 2, 'c': 3 };
for (var key in myObject) {
if (myObject.hasOwnProperty(key)) {
myObject[key] *= 2;
}
}
console.log(myObject);
Example 4: map object
let myMap = new Map()
let keyString = 'a string'
let keyObj = {}
let keyFunc = function() {}
myMap.set(keyString, "value associated with 'a string'")
myMap.set(keyObj, 'value associated with keyObj')
myMap.set(keyFunc, 'value associated with keyFunc')
myMap.size
myMap.get(keyString)
myMap.get(keyObj)
myMap.get(keyFunc)
myMap.get('a string')
myMap.get({})
myMap.get(function() {})