将数组推入JSON对象,Javascript(Push array into JSON Object, Javascript)

我正在努力编写一个有角度的应用程序。 将空数组推入对象时遇到问题。

我收到一个错误:

TypeError:无法读取未定义的属性“push”

我有一个名为items的对象,如下所示:

Object { "Name": "name", "Description": "description" }

我想将一个空数组推入可以包含另一个数组的对象。 像这样的东西。

Object { "Name": "name", "Description": "description", "Related Items": { Item1:{...}, Item2:{...}, ... } }

我的控制器在调用时执行此操作:

$scope.push = function () { $scope.item.push({"Related Items":[]}); };

我知道我必须混淆JSON对象和数组的简单方法,但我似乎无法找到解决方案。

谢谢!

I am trying my best to program an angular app. I have a problem pushing an empty array into an object.

I get an error:

TypeError: Cannot read property 'push' of undefined

I have an object called items which looks like this:

Object { "Name": "name", "Description": "description" }

I would like to push an empty array into the object that can contain another array. Something like this.

Object { "Name": "name", "Description": "description", "Related Items": { Item1:{...}, Item2:{...}, ... } }

My controller does this when it is called:

$scope.push = function () { $scope.item.push({"Related Items":[]}); };

I know I must be getting mixed up with something simple about the JSON Objects and Arrays, but I can't seem to find a solution.

Thankyou!

最满意答案

由于item是一个对象,您只需设置Related Items属性:

$scope.item["Related Items"] = []; $scope.item["Related Items"].push({});

但是,上面看起来像Related Items实际上是一个具有键名称Item1等的对象而不是数组。

$scope.item["Related Items"] = {}; $scope.item["Related Items"].Item1 = {};

Since item is an object, you can just set the Related Items property:

$scope.item["Related Items"] = []; $scope.item["Related Items"].push({});

However, above it looks like Related Items is actually an object with key names Item1, etc. rather than an array.

$scope.item["Related Items"] = {}; $scope.item["Related Items"].Item1 = {};将数组推入JSON对象,Javascript(Push array into JSON Object, Javascript)

我正在努力编写一个有角度的应用程序。 将空数组推入对象时遇到问题。

我收到一个错误:

TypeError:无法读取未定义的属性“push”

我有一个名为items的对象,如下所示:

Object { "Name": "name", "Description": "description" }

我想将一个空数组推入可以包含另一个数组的对象。 像这样的东西。

Object { "Name": "name", "Description": "description", "Related Items": { Item1:{...}, Item2:{...}, ... } }

我的控制器在调用时执行此操作:

$scope.push = function () { $scope.item.push({"Related Items":[]}); };

我知道我必须混淆JSON对象和数组的简单方法,但我似乎无法找到解决方案。

谢谢!

I am trying my best to program an angular app. I have a problem pushing an empty array into an object.

I get an error:

TypeError: Cannot read property 'push' of undefined

I have an object called items which looks like this:

Object { "Name": "name", "Description": "description" }

I would like to push an empty array into the object that can contain another array. Something like this.

Object { "Name": "name", "Description": "description", "Related Items": { Item1:{...}, Item2:{...}, ... } }

My controller does this when it is called:

$scope.push = function () { $scope.item.push({"Related Items":[]}); };

I know I must be getting mixed up with something simple about the JSON Objects and Arrays, but I can't seem to find a solution.

Thankyou!

最满意答案

由于item是一个对象,您只需设置Related Items属性:

$scope.item["Related Items"] = []; $scope.item["Related Items"].push({});

但是,上面看起来像Related Items实际上是一个具有键名称Item1等的对象而不是数组。

$scope.item["Related Items"] = {}; $scope.item["Related Items"].Item1 = {};

Since item is an object, you can just set the Related Items property:

$scope.item["Related Items"] = []; $scope.item["Related Items"].push({});

However, above it looks like Related Items is actually an object with key names Item1, etc. rather than an array.

$scope.item["Related Items"] = {}; $scope.item["Related Items"].Item1 = {};