1

I have this:

var dinosaursObjectArray = [
  { name: "Tyrannosaurus Rex", period: "Late Cretaceous" },
  { name: "Stegosaurus", period: "Late Jurassic" },
  { name: "Plateosaurus", period: "Triassic" }
];

var dinoArray = dinosaursObjectArray[0];
alert(dinoArray);

but the outcome is this [object object]

How to get the properties of the object?

Thank you

1
  • replace alert(dinoArray); with console.log(dinoArray);, press F12 to open the console and you'll see it. Don't use alert for debugging Commented Aug 26, 2017 at 18:23

4 Answers 4

2

The alert function gets a string, so when you passed the object to that function you got the string-representation of that object.

You actually did get the object you were looking for, but you just didn't know it because you used alert for debugging (which is not a good practice). You better use console.log for that (you can open the developer toolbar to see the results of console.log using F12).

If you want you can convert the object to json string (using JSON.stringify) and alert the result:

var dinosaursObjectArray = [
    { name: "Tyrannosaurus Rex", period: "Late Cretaceous" },
    { name: "Stegosaurus", period: "Late Jurassic" },
    { name: "Plateosaurus", period: "Triassic" }
];

var dinoArray = dinosaursObjectArray[0];
console.log(dinoArray);
alert(JSON.stringify(dinoArray));

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, oke. Thank you. nice
0

Here you with a solution

var dinosaursObjectArray = [
  { name: "Tyrannosaurus Rex", period: "Late Cretaceous" },
  { name: "Stegosaurus", period: "Late Jurassic" },
  { name: "Plateosaurus", period: "Triassic" }
];

var dinoArray = dinosaursObjectArray[0];
console.log("Name: ", dinoArray.name, ", Period: ", dinoArray.period);

For accessing a value inside an object please use key.

Hope this will help you.

Comments

0

Use JSON.stringify() method to converts a JavaScript value to a JSON string. Please follow this : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

If you want to get properties of the object you can do browser level debugging using console.log instead of alert. That's the best way to get properties of the object.

var dinoArray = dinosaursObjectArray[0];
console.log(dinoArray);

Comments

0

This is general solution how to access properties of an object. Note that in OP dinoArray.name and dinoArray['name'] are equivalent.

var dinosaursObjectArray = [
                            { name: "Tyrannosaurus Rex", period: "Late Cretaceous" },
                            { name: "Stegosaurus", period: "Late Jurassic" },
                            { name: "Plateosaurus", period: "Triassic" }
          ];
var obj = dinosaursObjectArray[0];

for(var key in obj){
console.log(key + ': ' + obj[key]);
}

Comments

Your Answer

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.