0

I have and object I would to extract "error" form.

[{"name":"Whats up","error":"Your name required!"}]

The inspector renders object format

[Object]
     0: Object
          error: "Your name required!"
          name: "Whats up"

How do I extract the error only if I do not know object names. I tried Object.Object[0].error

2 Answers 2

1

Your object is in a list - hence the square brackets. So, take the first element of the list, and then access its property:

var yourObject = [{"name":"Whats up","error":"Your name required!"}];

var firstElement = yourObject[0];  // firstElement is now = {"name":"Whats up","error":"Your name required!"}
console.log(firstElement.error);

// or immediately:
console.log(yourObject[0].error);

Example in JSFiddle: https://jsfiddle.net/hsg7qe87/

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

Comments

0

This is how you'd access parts of a JSON object.

var obj = [{"name":"Whats up","error":"Your name required!"}];

console.log(obj[0]);
> {"name":"Whats up","error":"Your name required!"}

console.log(obj[0].name);
> "Whats up"

So you'd just use:

obj[0].error;

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.