javascript将对象添加到数组中

时间:2018-05-07 11:13:34

标签: javascript arrayobject

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

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

我希望数组像[{来源:" LMP"},{目的地:" LKO"}]; 即我想将Id作为对象的关键

谢谢你!

2 个答案:

答案 0 :(得分:1)

您似乎希望将id作为对象的键。在ID

周围使用[]
this.journeyIds = ["source", "destination"];
this.journeyDetails = [];

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

答案 1 :(得分:0)

我没有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)



where left('ABC1001Z', 3) between left(start, 3) and left(end, 3) and
      substr('ABC1001Z', 4, 4) + 0 between substr(start, 4, 4) + 0 and substr(end, 4, 4) + 0