Thanks to another SO thread, I have managed to transform an array of arrays into an object thanks to reduce(). However, I don't understand why it works.
Could someone please explain to me why I can't only write => acc[key] = value, {} ? This syntax makes sense to me. But it returns 21! It only works if I add ,acc between my object assignation and my starting value {}. Also, ESLint claims it is an unexpected use of comma operator.
const myArray = [["player1", 30], ["player2", 21]];
const obj = myArray.reduce((acc, [key, value]) => (acc[key] = value, acc), {});
console.log("obj : ", obj);
I have also written this. It works well as well, but I'm still wondering why the first script works:
const myArray = [["player1", 30], ["player2", 21]];
const obj = myArray.reduce((acc, [key, value]) => Object.assign(acc, {[key]: value}),{});
console.log("obj : ", obj);
, accwill returnaccobject. onlyacc[key] = valuewill returnvaluenotacc.