Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Javascript object value by key

Tags:

javascript

var Array = [];

{'DateOfBirth' : '06/11/1978',
 'Phone' : '770-786',
 'Email' : '[email protected]' ,
 'Ethnicity' : 'Declined' ,
 'Race' : 'OtherRace' , }

I need to access the 'Race' here.. how can i do it... Its an array which holds this data...

like image 210
John Cooper Avatar asked Jun 26 '26 06:06

John Cooper


1 Answers

Thats not an array, its an object. You want to do something like:

var myObject = {
  'DateOfBirth' : '06/11/1978',
  'Phone' : '770-786',
  'Email' : '[email protected]' ,
  'Ethnicity' : 'Declined' ,
  'Race' : 'OtherRace'
};

// To get the value:
var race = myObject.Race;

If the Objects are inside an array var ArrayValues = [{object}, {object}, ...]; then regular array accessors will work:

var raceName = ArrayValues[0].Race;

Or, if you want to loop over the values:

for (var i = 0; i < ArrayValues.length; i++) {
    var raceName = ArrayValues[i].Race;
}

Good documentation for arrays can be found at the Mozilla Developer Network

like image 122
minichate Avatar answered Jun 29 '26 20:06

minichate