0

In the example that follows I get as a result the word 'object' but I cannot succeed in getting the objects themselves. Could you please someone help me ?

<div id="myResult"></div>

<script>
myArray = [1, 3, 5]

var lengths = myArray.map(function (e, i) {
    return {index: i, value: e.length };
});

document.getElementById('myResult').innerHTML = lengths;

// Output
// [object Object],[object Object],[object Object]
</script>
Apostolos
  • 598
  • 1
  • 5
  • 13

1 Answers1

1

The comment by @Shubh is correct. You need to turn your lengths array of object into a JSON before setting the innerHTML to lengths.

By using JSON.stringify it turns the object into a string type.

myArray = [1, 3, 5]

var lengths = myArray.map(function (e, i) {
    return {index: i, value: e.length };
});

document.getElementById('myResult').innerHTML = JSON.stringify(lengths);

Mohib Arshi
  • 830
  • 4
  • 13