-1
this.journeyIds = ["source", "destination"];
this.journeyDetails = [];

this.journeyIds.map((id)=>{
    this.journeyDetails.push({
        id: this.el("#" + id).inputValue
    });
});

I want array like [{Source : "LMP"}, {Destination : "LKO"}]; i.e I want to make Id as key in object

thank you!

Rainbow
  • 6,772
  • 3
  • 11
  • 28
nayan verma
  • 103
  • 1
  • 11

2 Answers2

1

It seems that you want the id as key of an object. Use [] around the id

this.journeyIds = ["source", "destination"];
this.journeyDetails = [];

this.journeyIds.map((id) => {
                this.journeyDetails.push({[id] :
this.el("#"+id).inputValue});
            });
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
0

I don't have the function this.el() so it's an array here, you could just replace it with the function call (this.el["#"+id].inputValue => this.el("#"+id).inputValue

this.journeyIds = ["source", "destination"];
this.journeyDetails = [];
this.el = {
  "#source": {inputValue: "foo"},
  "#destination": {inputValue: "bar"}
}

this.journeyIds.forEach((id) => {
    let temp = {};
    temp[id] = this.el["#"+id].inputValue;
    this.journeyDetails.push(temp);
});

console.log(this.journeyDetails)
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44