简体   繁体   中英

Accessing value from key with JavaScript

I'm new to JavaScript. This is giving me undefined .

What am I doing wrong? How do I log the values inside the objects?

 let spaceship = { passengers: null }; (spaceship.passengers = [{ name: "Mike" }, { ability: "teleportation" }]), [{ name: "David" }, { ability: "cook" }]; let firstPassenger = spaceship.passengers[0]; console.log(firstPassenger.ability);

For clarity, this statement:

(spaceship.passengers = [{ name: "Mike" }, { ability: "teleportation" }]),
  [{ name: "David" }, { ability: "cook" }];

is syntactically like

(foo = 1), 2;

This assigns 1 to foo , not [1, 2] as you expect. In your example, you assign to spaceship.passengers the value [{ name: "Mike" }, { ability: "teleportation" }] . Your firstPassenger is thus { name: "Mike" } , and has no ability . The one with the ability is your second, nameless passenger.

What you likely wanted to write instead is:

spaceship.passengers = [{ name: "Mike", ability: "teleportation" },
  { name: "David", ability: "cook" }];

or formatted a bit more nicely:

spaceship.passengers = [
  { name: "Mike", ability: "teleportation" },
  { name: "David", ability: "cook" },
];

It might sound preachy, but good formatting is key to good programming, as it allows you to see the structure of your code at a glance. It is clear in my last example what is the first passenger; in yours, not so much.

I think you are defining the array of objects the wrong way, I think you want the ability and name into one object:

 let spaceship = { passengers: null }; spaceship.passengers = [{ name: "Mike" , ability: "teleportation" }, { name: "David" , ability: "cook" }]; let firstPassenger = spaceship.passengers[0]; console.log(firstPassenger.ability);

I guess what you want to do, do you want to set the value of the object?

I have provided the following example, you can try to compare the two and confirm whether this is what you want

Regarding your json string, I suggest that you redesign it as in my example. This is because all the attributes of an object are best placed in an object. This will make the scope clearer.

 let spaceship = { passengers: null }; spaceship.passengers = [{ name: "Mike", ability: "teleportation" }, { name: "David", ability: "cook" }]; const passengers = spaceship.passengers; passengers.forEach(obj => console.log(`name: ${obj.name}, ability: ${obj.ability}`) );

You defined object in a wrong way. You should define object like this to get the desired result

let spaceship = {
    passengers: null
  };

        spaceship.passengers = [
            {
               name: "Mike" , ability: "teleportation" 
            },
            {
               name: "David" , ability: "cook" 
            }
          ];
     let firstPassenger = spaceship.passengers[0];
          console.log(firstPassenger.ability);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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