Can someone please explain why obj[user.name] = user.age is used instead of user[‘name’] : user.age as I can read user.age is being assigned to user.name.
Second, why accumulater ’ {}’ is used since we are not doing any computation and not required any initial value.
Third, please explain this syntax in details as I am confuse now with little knowledge. Your code so far
WARNING
The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.
You will need to take an additional step here so the code you wrote presents in an easy to read format.
Please copy/paste all the editor code showing in the challenge from where you just linked.
Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Challenge: Functional Programming - Use the reduce Method to Analyze Data
I will use either way to access object IN an array :
users[0][‘name’]
users[0].name
But javascript has really convenient array-method to use instead of using forloop to iterate the arrayitem inside such as:
Array.forEach()
Array.map()
Array.reduce()
Array.filter()
Each of them has different use case. You can read more here > JavaScript Array Iteration
And when you use this Array iteration method, the access to the key of object is easier, like treating normal object:
Why an empty object?
Well , it depends. I think the example just to avoid confusion, so initialize with empty object.
I understood it like an empty Array. Since i would like to return a new Array, better i initialise it as empty, then push( ) in the result of each iteration.
You can always console.log(obj ) out to see, how obj changes over each iteration.
Seems like a new obj will be returned and replace the old one, and continue to be replaced by next iteration. And eventually return the final obj.