0

I have a JavaScript array and I want to get the value of last name from it. Can anyone tell how to get that from this array example:

var result = [{"FirstName":"paapu","LastName":"gandhi"}];
1
  • Just write result[0].LastName Commented Feb 2, 2016 at 10:44

2 Answers 2

1

You have an array containing an object, so you have to retrieve the object by doing:

var myObj = result[0]

And then get the LastName property by:

var lastname = myObj.LastName
Sign up to request clarification or add additional context in comments.

Comments

0

Get the first object.

var obj = result[0];

Refer to the property of the object:

var prop = result[0].FirstName;

If property name comes dynamically, that is, from a variable, use square bracket notation.

var myVar = "FirstName";
var prop = result[0][myVar];

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.