2

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}],
}
1
  • Your array contains just arrays - they are not named smallArr, thisArr or thatArr any more. If bigArr is all you have, then you will need to spell out the property names explicitly. Commented May 27, 2019 at 5:20

2 Answers 2

3

You can simply use object literal shorthand property

let smallArr = [1, 23, 25, 46, 52, 789, 64];
let thisArr =  [5, 4, 65, 24, 35];
let thatArr = [{key: 1, anotherKey: 2}];
let parentObj = { smallArr, thisArr, thatArr,}

console.log(parentObj)

Sign up to request clarification or add additional context in comments.

Comments

0

Just explicitly assign each variable:

const smallArr = [1, 23, 25, 46, 52, 789, 64];
const thisArr = [5, 4, 65, 24, 35];
const thatArr = [{key: 1, anotherKey: 2}];
let parentObj = { smallArr, thisArr, thatArr };

console.log(parentObj);
.as-console-wrapper { max-height: 100% !important; top: auto; }

The above is shorthand for:

const smallArr = [1, 23, 25, 46, 52, 789, 64];
const thisArr = [5, 4, 65, 24, 35];
const thatArr = [{key: 1, anotherKey: 2}];
let parentObj = { smallArr: smallArr, thisArr: thisArr, thatArr: thatArr };

console.log(parentObj);
.as-console-wrapper { max-height: 100% !important; top: auto; }

2 Comments

I did not mean to, I tried to check both answers because they were the same and I guess it downvoted yours when I checked the second
It's OK - you can accept the other one @DoubleBridges. Just un-accept mine first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.