The JavaScript code below is supposed to populate the array personNames
with objects containing names from an array of persons. However, it incorrectly repeats the same name for each object instead of assigning different names:
[{"name":"smith"},{"name":"smith"},{"name":"smith"}]
Instead, it should be generating an array like this:
[{"name":"john"},{"name":"doug"},{"name":"smith"}]
var personNames = [];
var personName = {};
var persons = [{"firstname": "john", "id": "111"}, {"firstname": "doug", "id": "777"}, {"firstname": "smith", "id": "888"}];
if(persons.length > 0){
array.forEach(persons, function(person){
personName.name = person.firstname;
personNames.push(personName);
});
}