I am trying to work with an array of objects where each object contains some arrays. I want to easily access the values within these arrays, such as multiplying them by a constant.
It seems like using forEach()
might be the best approach. Here is how I attempted it:
myArray = [{
income: [1, 2, 3],
outcome: [2, 3]
},
{
income: [1, 9, 8, 5],
outcome: [1, 3, 7]
},
{
income: [7, 2, 8],
outcome: [2, 6, 10]
},
];
const myValue = 2;
myArray.forEach(ob => ob.income = ob.income * myValue, ob.outcome = ob.outcome * myValue);
Based on this operation, the expected result should look like this:
myArray = [
{income: [2, 4, 6], outcome: [4, 6]},
{income: [2, 18, 16, 10], outcome: [2, 6, 14]},
{income: [14, 4, 16], outcome: [4, 12, 20]},
];