简体   繁体   English

Javascript-将数组推入对象中的数组

[英]Javascript - Push an Array into array in object

I have problem to push some value that assigned to be an array in the object 我有问题推分配给在所述对象的阵列的一些值

Here my code : 这是我的代码:

var hasil = [{  product: 'listBarang[i][0]',
                shoppers: [],
                leftOver: 'listBarang[i][2]',
                totalProfit: 0
            }]

What I think is using push method like below 我认为正在使用如下的推方法

hasil.shoppers.push('test')

But it give me an error like this 但这给我一个错误

TypeError: Cannot read property 'push' of undefined TypeError:无法读取未定义的属性“ push”

Is anyone know how to deal with this? 有人知道如何处理吗?

hasil是一个对象数组,因此,如果要处理这些对象,则需要直接访问它们,即使用hasil[0]

hasil[0].shoppers.push('test')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM