值为array的Javascript动态属性在推送后为空

值为array的Javascript动态属性在推送后为空,javascript,angularjs,typescript,ionic3,Javascript,Angularjs,Typescript,Ionic3,这太疯狂了。我从Web服务器读入一个对象并在其中循环,我可以向该对象添加属性。但是当我试着自己制作一张新地图时,它不起作用。尽我所能的简单 // from the server and works plans: any[]; sourceByPlan: any[]; // my own map for other purposes I try to fill // as I loop through the items from the server customMap: any[]; //

这太疯狂了。我从Web服务器读入一个对象并在其中循环,我可以向该对象添加属性。但是当我试着自己制作一张新地图时,它不起作用。尽我所能的简单

// from the server and works
plans: any[];
sourceByPlan: any[];

// my own map for other purposes I try to fill
// as I loop through the items from the server
customMap: any[]; // also tried 'any'

var res = JSON.parse(response);
this.plans = res.plans; // where res.plans looks like [{value: "id", label: "readableid"}, {value: "id2", label: "otherreadable"}]
this.sourceByPlan = res.sources; // where res.sources looks like { id: [Object, Object, Object...]};

// loop through each source and add a property
for(var i = 0; i < this.plans.length; i++){
    if(this.sourceByPlan[this.plans[i].value]){
        for (var j = 0; j < this.sourceByPlan[this.plans[i].value].length; j++){
            var sObj = this.sourceByPlan[this.plans[i].value][j];

            var status = this.getStatus(sObj); // simple string
            sObj["newstatus"] = status; // THIS WORKS AS EXPECTED


            // here is where the new non working code starts
            var key = this.plans[i].value + status ; // looks like 'asdf1234pending'

            if (!customMap[key]) {
                customMap[key] = [];
            }

             customMap[key].push(sObj); // this will still be empty
        }   
    }
}
//从服务器下载并运行
计划:任何[];
sourceByPlan:任何[];
//我自己的地图,用于其他目的,我尝试填充
//当我从服务器循环浏览项目时
自定义地图:任意[];//还尝试了“任何”
var res=JSON.parse(响应);
this.plans=res.plans;//其中res.plans看起来像[{value:“id”,label:“readableid”},{value:“id2”,label:“otherreadable”}]
this.sourceByPlan=res.sources;//其中res.sources看起来像{id:[对象,对象,对象…]};
//循环遍历每个源并添加属性
对于(var i=0;i

sObj没有从服务器附带属性sObj.newstatus,但它允许我根据需要添加它。我正在尝试动态构建另一个地图,但每次检查console.log(customMap)时,它都是空的。从逻辑上讲,它看起来与我从服务器获取的对象相同

您正在将未定义的对象推送到自定义地图。您可能想使用sObj:

customMap[key].push(sObj);

什么是obj
?我看不到任何其他的参考资料。这应该是
sObj
?是的,我会编辑它,对于一个简单的打字错误,这应该是一个注释。@ScottMarcus你为什么这么认为?因为这个问题是一个简单的打字错误,这实际上是我们结束问题的原因之一。正如你所看到的,@Pointy做了正确的事情。这是我们处理此类问题的一般方法。@ScottMarcus下次会这样做!我会把我的答案留在这里,让其他人也知道。事实上,你最好删除你的答案。不是每个人都读评论,避免其他人做同样事情的最好方法是不要有这样做的例子