0

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'
}

5 Answers 5

1

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. this works fine for me. With very less code, i love it.
1

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);

Comments

1

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));

2 Comments

Is it related with the question?
What do you mean ?
0

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);

Comments

0

If you are on typescript or ES6 you can make the suggestion from @Álvaro Touzón even easier:

let re = vehicleAvailabilities.map(item => item.weekday = weekdayMap[item.day])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.