2017-10-04 93 views
-4

我有这样的:如何用键推入对象数组?

public items: any[] = []; 

什么我尝试做:

this.events.map((event) => { 
     this.items[event.item] = event; 
    }); 

但我总是得到一个项目的每一个关键。任何建议如何推动它在这些项目?

在事件中,我得到这样的:

{item: "9000225-01/17", type: "ORDER", status: "0", trackingId: 6033, statusName: "U pripremi", …} 
    {item: "9000225-01/17", type: "ORDER", status: "1", trackingId: 6035, statusName: "Primljena", …} 
    {item: "9000225-01/17", type: "ORDER", status: "8", trackingId: 6036, statusName: "TM ne postoji", …} 
    {item: "9000225-01/17-01", type: "ORDITEM", status: "0", trackingId: 6034, statusName: "U pripremi", …} 

,现在我想的是插入在items[event.item]所有对象具有从上面这个对象相同item

+0

你用'.push()'试过吗? – JJJ

+0

我得到undefined this.items [event.item]但当我控制台日志event.item和this.items ai获取数据 – None

回答

1

您需要使用.push

this.events.map(event => { 
    if (this.items[event.item]) { 
    this.items[event.item].push(event); 
    } else { 
    this.items[event.item] = [event]; 
    } 
}); 
+0

当我这样做的项目是空的最后它不输入在这个if语句 – None

+0

那么你arae期待?我 – Sajeetharan

+0

我发布我的对象什么事件返回。我想在项目插入基于event.item,所以在我this.items我会有:[9000225-01/17],那里我将有三个对象和[9000225-01/17-01]我将有一个对象 – None

0
this.items[event.item].push(event); 
+0

我得到undefined this.items [event.item] – None

+0

this.items [event.item] = [ ]。 this.items [event.item] .push();这是非常基本的javascript – Carsten

+0

我没有看到event.item它在this.events.map – None