如何用键推入对象数组?

时间:2017-10-04 11:03:01

标签: angular

我有这个:

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的所有对象。

2 个答案:

答案 0 :(得分:1)

您需要使用.push

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

答案 1 :(得分:0)

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