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"}];
Misa Lazovic
2,83110 gold badges34 silver badges39 bronze badges
asked Feb 2, 2016 at 10:25
Tulika Kansal
571 silver badge8 bronze badges
2 Answers 2
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
answered Feb 2, 2016 at 10:28
Alberto Nicoletti
1363 silver badges5 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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];
answered Feb 2, 2016 at 11:11
Akshay Arora
1,9551 gold badge16 silver badges31 bronze badges
Comments
lang-js
result[0].LastName