将变量推入数组中的辅助子对象

时间:2019-07-15 09:27:16

标签: javascript angular typescript

当用户点击添加按钮时,我试图将变量添加到从API接收的现有JSON数据中。当我尝试我得到这个错误。

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays

数据被分配给名为pageList的变量 下面是如何从服务器返回数据

数据

[{"id":5,"referenceId":1189,"firstName":"Dan","lastName":"Daniels","orders":[{"id":109,"meals":[{"id":47,"name":"Fried Rice","description":"This is a  very sweet meal","image":"","mealType":"LUNCH","unitPrice":-20,"status":"ENABLED"}],"serveDate":"2019-07-11 00:00:00"}]}]

功能

AddtoList() {
       const meals =  {'id': '0','name': 'New Food', 'unitPrice':'5'}
       this.pageList.push({
                  'firstName': 'Jackson',
                  'lastName': 'Jack',
                  orders: {meals: meals}
                })
}

2 个答案:

答案 0 :(得分:1)

将订单对象更新为数组。

AddtoList() {
       const meals =  {'id': '0','name': 'New Food', 'unitPrice':'5'}
       this.pageList.push({
         'firstName': 'Jackson',
         'lastName': 'Jack',
         orders: [{meals: meals}]
        })
}

答案 1 :(得分:1)

我认为orders应该是一个数组。请看如下:

this.pageList.push({
  'firstName': 'Jackson',
   'lastName': 'Jack',
   orders: [{meals: meals}]
})