I need a solution to add some values from an Object ( weekdayMap ) into an existing array ( vehicleAvailabilities ) with objects in it. On day one i need the value Montag on day 2 the value Dienstag
and so on
I need the result like this:
const result = [
{id: 1, day: "1", value: true, weekday: 'Montag'},
{id: 2, day: "2", value: true, weekday: 'Dienstag'} ...
from this both:
const vehicleAvailabilities = [
{id: 1, day: "1", value: true},
{id: 2, day: "2", value: true},
{id: 3, day: "3", value: true},
{id: 4, day: "4", value: true},
{id: 5, day: "5", value: true},
{id: 6, day: "6", value: false},
{id: 7, day: "7", value: false}
]
const weekdayMap = {
1: 'Montag',
2: 'Dienstag',
3: 'Mittwoch',
4: 'Donnerstag',
5: 'Freitag',
6: 'Samstag',
7: 'Sonntag'
}
here is a demo , i love array map and all new functional code in js
const vehicleAvailabilities = [
{id: 1, day: "1", value: true},
{id: 2, day: "2", value: true},
{id: 3, day: "3", value: true},
{id: 4, day: "4", value: true},
{id: 5, day: "5", value: true},
{id: 6, day: "6", value: false},
{id: 7, day: "7", value: false}
]
const weekdayMap = {
1: 'Montag',
2: 'Dienstag',
3: 'Mittwoch',
4: 'Donnerstag',
5: 'Freitag',
6: 'Samstag',
7: 'Sonntag'
}
let re = vehicleAvailabilities.map(function(item){
item.weekday = weekdayMap[item.day];
return item;
})
console.log(re);
Make your own function for it:
const vehicleAvailabilities = [ {id: 1, day: "1", value: true}, {id: 2, day: "2", value: true}, {id: 3, day: "3", value: true}, {id: 4, day: "4", value: true}, {id: 5, day: "5", value: true}, {id: 6, day: "6", value: false}, {id: 7, day: "7", value: false} ] const weekdayMap = { 1: 'Montag', 2: 'Dienstag', 3: 'Mittwoch', 4: 'Donnerstag', 5: 'Freitag', 6: 'Samstag', 7: 'Sonntag' } function mergeMapIntoArray(map, array) { return array.map((entry) => { entry.weekday = map[entry.id]; return entry; }); } console.log(mergeMapIntoArray(weekdayMap, vehicleAvailabilities));
ES6 solution.
const a = [{id:1,day:"1",value:true},{id:2,day:"2",value:true},{id:3,day:"3",value:true},{id:4,day:"4",value:true},{id:5,day:"5",value:true},{id:6,day:"6",value:false},{id:7,day:"7",value:false}]; const b = {1:'Montag',2:'Dienstag',3:'Mittwoch',4:'Donnerstag',5:'Freitag',6:'Samstag',7:'Sonntag'}; const r = Object.keys(b).map((v, i) => ({ weekday: b[v], ...a[i] })); console.log(r);
Here an example script that using the map and simply add a new property to the objects.
var vehicleAvailabilities = [ {id: 1, day: "1", value: true}, {id: 2, day: "2", value: true}, {id: 3, day: "3", value: true}, {id: 4, day: "4", value: true}, {id: 5, day: "5", value: true}, {id: 6, day: "6", value: false}, {id: 7, day: "7", value: false} ]; const weekdayMap = { 1: 'Montag', 2: 'Dienstag', 3: 'Mittwoch', 4: 'Donnerstag', 5: 'Freitag', 6: 'Samstag', 7: 'Sonntag' } var result = []; var item; for(var i=0;i<vehicleAvailabilities.length;i++){ item = vehicleAvailabilities[i]; item.weekday = weekdayMap[parseInt(item.day)]; result.push(item); } console.log(result);
如果您使用打字稿或ES6,则可以使@ÁlvaroTouzón的建议更加容易:
let re = vehicleAvailabilities.map(item => item.weekday = weekdayMap[item.day])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.