Shouldn’t it be transaction.amount? Instead of amount?
https://www.freecodecamp.org/learn/full-stack-developer/lecture-working-with-higher-order-functions-and-callbacks/what-is-method-chaining-and-how-does-it-work
const transactions = [
{ amount: 100, type: "credit" },
{ amount: 20, type: "cash" },
{ amount: 150, type: "credit" },
{ amount: 50, type: "cash" },
{ amount: 75, type: "credit" }
];
const totalCreditWithBonus = transactions
.filter((transaction) => transaction.type === "credit")
.map((transaction) => transaction.amount * 1.1)
.reduce((sum, amount) => sum + amount, 0);
console.log(totalCreditWithBonus); // 357.5
In this line:
.reduce((sum, amount) => sum + amount, 0);
or transaction.amount
What does the map() return?
If you changed it to
const totalCreditWithBonus = transactions
.filter((transaction) => transaction.type === "credit")
.map((transaction) => transaction.amount * 1.1)
what is returned and stored in totalCreditWithBonus ?
1 Like
the array with each amount properly updated to *1.1
but thats not what im talking about
But it is exactly what you are talking about. The map method returns an array. The callback in the map methods says the new array is populated with the values transaction.amount * 1.1, which are numbers. Numbers will not have the .amount property.
1 Like
oh im sorry i understand now.
so it will create a new array of those values and no objects
The map method populates the new array with whatever is returned by the callback.
1 Like