I have write this code to access object value in loop but it won't let me access in loop I am getting console error but when I access each value individually I don't get any error.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var keys;
var obj = [{"id":"1","firstname":"Aftab","lastname":"Altaf"},{"id":"2","firstname":"Haris","lastname":"Jaliawala"},{"id":"3","firstname":"Muzammil","lastname":"Mumtaaz"}];
for(key in obj)
{
keys = Object.keys(obj[key]);
}
console.log(obj[0].firstname);
for(value in obj)
{
console.log(obj[value].keys[value]);
}
</script>
</body>
</html>
This is the output I am getting in my console. OUTPUT
Aftab
Uncaught TypeError: Cannot read property '0' of undefined
Anyone please help?
objis an array, doesn't matter if you call itobj, it's still an array, and arrays are iterated with regular for loops, and the iteration only stores the last iteratet keys in the variablekeys, not all of them, so it's basically all just wrong.