I have an array of objects that looks like this:
[ { firstName: 'Mike', lastName: 'Jones' },
{ firstName: 'Joe', lastName: 'Smith' },
{ firstName: 'Bob', lastName: 'Johnson' } ]
I need to pass that array to a function that's going to add a "middleName" field to each object, and a value for middleName. The function gets the middle name by making an asynchronous http.get request in Node. And therein lies the problem. I can't make this work no matter how I try.
Previously someone suggested a loop like this:
array.forEach(function (obj) {
GetMiddleName(obj, function (person) {
obj.MiddleName = person;
});
});
But that doesn't work due to the async nature of the get being called in the GetMiddleName function. Can anyone show me a short, simple function that will do what I need?