How to map an array of objects code example
Example 1: javascript create array of objects with map
var arr = [{
id: 1,
name: 'bill'
}, {
id: 2,
name: 'ted'
}]
var result = arr.map(person => ({ value: person.id, text: person.name }));
console.log(result)
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: javascript map
function listFruits() {
let fruits = ["apple", "cherry", "pear"]
fruits.map((fruit, index) => {
console.log(index, fruit)
})
}
listFruits()
Example 4: javascript map array
const sweetArray = [2, 3, 4, 5, 35]
const sweeterArray = sweetArray.map(sweetItem => {
return sweetItem * 2
})
console.log(sweeterArray)
Example 5: functions in map javascript
const map = new Map();
function foo() {
return "Hello World!";
}
map.set("foo", foo);
console.log(map.get("foo")());
Example 6: map a property from array of objects javascript
var result = objArray.map(function(a) {return a.foo;});