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

    1
    • 1
      Thanks. this works fine for me. With very less code, i love it.
      – pkberlin
      CommentedNov 10, 2017 at 12:32
    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); 
      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
      • Is it related with the question?
        – kind user
        CommentedNov 10, 2017 at 12:20
      • What do you mean ?CommentedNov 10, 2017 at 12:24
      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);

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

          Start asking to get answers

          Find the answer to your question by asking.

          Ask question

          Explore related questions

          See similar questions with these tags.