get the first object from array js code example
Example 1: js get first object value
var obj = { first: 'someVal' };
obj[Object.keys(obj)[0]];
Example 2: Return the first element of the array javascript
var studentDetails=
[
{
"firstName":"John"
},
{
"firstName":"David"
},
{
"firstName":"Bob"
},
{
"firstName":"Mike"
},
{
"firstName":"Carol"
}
]
var firstObjectValue = "";
if(studentDetails.length > 1){
for(var index=0;index< studentDetails.length;index++){
firstObjectValue=studentDetails[index].firstName;
break;
}
} else {
firstObjectValue=undefined;
}
if(firstObjectValue!=undefined)
console.log(firstObjectValue);
else
console.log("There is no element in the array");