1

I am trying to add new items in both the field 'name' and 'breed' individually by using dogs_info.name.push(_) but getting an error:

Cannot read property 'name' of undefined

I am hoping for an in-depth solution.

var dogs_info = [
 {
 name : "rusty",
 breed : "corgi"
 },
 {
 name: "snoopy",
 breed: "pomperian"
 },
 {
 name: "ropper",
 breed: "husky"
 },
 {
 name: "maya",
 breed: "pomsky"
 },
 {
 name: "lara",
 breed: "alskan malamute"
 }
]
Barmar
789k57 gold badges555 silver badges669 bronze badges
asked Jul 7, 2019 at 12:16
2
  • 1
    You're trying to access the property name of an array, which doesn't exist... You're also trying to push into a (random?) object which is not possible, since push is an array method. Not sure what your goal is. Commented Jul 7, 2019 at 12:20
  • 2
    I also wonder how you're getting the error you're getting. I would expect the error to be Cannot read property 'push' of undefined, since dogs_info is definitely defined. Commented Jul 7, 2019 at 12:27

1 Answer 1

3

dogs_info has no property called name, so dogs_info.name gives you undefined.

dogs_info is an array of objects. The properties on those objects are not arrays.

It sounds like you may just want to add a new entry to the array, e.g.:

dogs_info.push({
 name: "fido",
 breed: "wee brun dug"
});

Note that there are no "nested arrays" in your example. You have a single array, which contains objects with name and breed properties (whose values are strings).

answered Jul 7, 2019 at 12:20
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.