I'm iterating through an array of arrays using .map() in an attempt to assign the inner arrays to an object as a key:value pair. It is not successful. Can this even work? Do I need to break it out into more steps/functions/variables?
I've tried every syntax I can think of for the element in the assignment of the property.
I also tried using arr.map(function() {}) syntax with all of these, and there was no difference.
const smallArr = [1, 23, 25, 46, 52, 789, 64];
const thisArr = [5, 4, 65, 24, 35];
const thatArr = [{key: 1, anotherKey: 2}];
let parentObj = {};
const bigArr = [smallArr, thisArr, thatArr];
const newSomething = bigArr.map(arr => parentObj["arr"] = arr);
// parentObj returns: { arr: [ { key: 1, anotherKey: 2 } ] }
I understand both of these. It's assigning the string of my element every iteration and overwriting the value so that the final object is one key:value pair with value being the last inner array in the outer array.
const newSomething = bigArr.map(arr => parentObj.arr = arr);
//returns { arr: [ { key: 1, anotherKey: 2 } ] }
const newSomething = bigArr.map(arr => parentObj['arr'] = arr);
//returns { arr: [ { key: 1, anotherKey: 2 } ] }
This one I don't understand what's going on in the last pair.
const newSomething = bigArr.map(arr => parentObj[`${arr}`] = arr);
// returns {
'1,23,25,46,52,789,64': [ 1, 23, 25, 46, 52, 789, 64 ],
'5,4,65,24,35': [ 5, 4, 65, 24, 35 ],
'[object Object]': [ { key: 1, anotherKey: 2 } ],
arr: [ { key: 1, anotherKey: 2 } ]
}
This one I don't understand at all.
const newSomething = bigArr.map(arr => parentObj["`${arr}`"] = arr);
// returns {
'`${arr}`': [ { key: 1, anotherKey: 2 } ],
arr: [ { key: 1, anotherKey: 2 } ]
}
I'm trying to get to:
parentObj = {
smallArr: [1, 23, 25, 46, 52, 789, 64],
thisArr: [5, 4, 65, 24, 35],
thatArr: [{key: 1, anotherKey: 2}],
}
smallArr,thisArrorthatArrany more. IfbigArris all you have, then you will need to spell out the property names explicitly.