Javascript Data Type How to - Access objects stored in an array








Question

We would like to know how to access objects stored in an array.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!-- w  w w  .j a  va  2s .  co  m-->
var myArray = [{first: 'v1', last: 'v2'}, {first: 'v3', last: 'v4'}];
var myFunc = function (values) {
    newArray = [];
    for (var i = 0; i < values.length; i++) {
        for(var e in values[i]) {
            newArray.push(values[i][e]);
        }
    }
    return newArray;
}
document.writeln(myFunc(myArray));

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: