I am trying to map an array of values to a property in an array of objects.
var myArray = ['2', '4', '7'];
var myProducts = [
{id: '755', price: '10'},
{id: '756', price: '20'},
{id: '757', price: '30'}
];
Please note that the length of myArray will always equal the number of objects in myProducts.
My first thought was to loop through objects in myProducts, and then mapping myArray to the shipping property inside that loop:
myProducts.forEach(function(obj) {
obj.shipping = Array.prototype.map(myArray);
});
But this isn't working, and I am also now questioning whether I should be using .map within the forEach loop. What's the best way to do this?
Desired result:
var myProducts = [
{id: '755', price: '10', shipping: '2'},
{id: '756', price: '20', shipping: '4'},
{id: '757', price: '30', shipping: '7'}
];