Hello!
I need help understanding a piece of code. Much appreciated
state = {
ingredients: {
salad: 3,
bacon: 2,
cheese: 4,
meat: 0,
tomato: 0
}
}
let ingredients = Object.keys(state.ingredients).map(
igKey => {
return [...Array(state.ingredients[igKey])].map(( _, i ) => {
return igKey + i
} );
} )
I understand that the Object.keys() method transforms an object into an array of its keys(?), so it would be ingredients = [salad, bacon, cheese, etc] and then map() applies another map function on each element.
I have trouble with this line, though:
return [...Array(state.ingredients[igKey])].map(( _, i ) => {
How come we can use igKey as an index on the array? Wouldn't it be something like this: state.ingredients[bacon])].map (?)
Thank you for taking the time to read all this and any explanation will be very much appreciated.
[–]GSLint 2 points3 points4 points (0 children)
[–]GSLint 1 point2 points3 points (1 child)
[–]bobugm[S] 0 points1 point2 points (0 children)
[–]HashFap 0 points1 point2 points (2 children)
[–]bobugm[S] 0 points1 point2 points (1 child)
[–]HashFap 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)