最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Push array into JSON Object, Javascript - Stack Overflow

matteradmin6PV0评论

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!

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!

Share Improve this question edited Oct 6, 2014 at 18:03 maikelinhas asked Oct 6, 2014 at 16:46 maikelinhasmaikelinhas 812 silver badges8 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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 = {};

Javascript's push function only works when you're pushing values to an array. It won't work if you try to push to an object, instead it will try to call the key of "push" which doesn't exist. That's why you're getting the error you're getting.

Make sure that $scope.item is an array ([] or new Array), and then push to it with the value you would like.

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

Here's W3School's .push() method explanation.

The item object should be like { .... "Related Items":<"some value"> .... }

It should already have key.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far