You’re not modifying your original array. You’re modifying the objects in the array. If you want to avoid mutating the objects in your array, you can use Object.assign
to create a new object with the original’s properties plus any changes you need:
const freeProduct = function(products) {
return products.map(x => {
return Object.assign({}, x, {
productType: "free"
});
});
};
2018 Edit:
In most browsers you can now use the object spread syntax instead of Object.assign
to accomplish this:
const freeProduct = function(products) {
return products.map(x => {
return {
...x,
productType: "free"
};
});
};