0

I'm trying to take a nested array and convert it into an object. I'm not understanding why i get undefined when I try to print the complex array.

My goal is to turn this:

var input = [
   [
       ['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']
   ],
   [
       ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']
   ]
];

Into this:

var result = [
    {firstName: 'Joe', lastName: 'Blow', age: 42, role: 'clerk'},
    {firstName: 'Mary', lastName: 'Jenkins', age: 36, role: 'manager'}
]

So far I have this. I know I'm going to need a nested loop but I can't get my console to display anything past undefined.

function transformEmployeeData(employeeData) {
    var output = {};
    var per = {};
    // console.log(per);
    // console.log(employeeData);
    console.log("ED: " + employeeData[0] + "End"); //Expecting the first array in input. Getting output: ED: End
    const obj = Object.assign({}, ...employeeData); 
    console.log("OBJ: " + obj);//Output: OBJ: [object Object]
    for (let i = 0; i < employeeData.length; i++) { //Haven't been able to get here because everything is undefined
        // console.log(employeeData[i][0] + " " + employeeData[i][1]);
        // per[employeeData[i][0][0]] = per[employeeData[i][0][0]] || {};
        // per[employeeData[i][0][0]] = employeeData[i][0][1];
    }
    // console.log("Output: " + namesObj);
}

I've been experimenting with this for an hour. I'm sure its some type of syntax error but I just don't see it.

Even a simple test of a small nested array is giving me an error.

var tests = [["first", "Joe"]["Tests", "two"]];
console.log(tests[0][0]);//TypeError: Cannot read property '0' of undefined 
Funlamb
  • 551
  • 7
  • 20

3 Answers3

5

You can use map() and Object.fromEntries()

var input = [
  [
    ["firstName", "Joe"],
    ["lastName", "Blow"],
    ["age", 42],
    ["role", "clerk"],
  ],
  [
    ["firstName", "Mary"],
    ["lastName", "Jenkins"],
    ["age", 36],
    ["role", "manager"],
  ],
];

const output = input.map(item => Object.fromEntries(item));

console.log(output);
ikhvjs
  • 5,316
  • 2
  • 13
  • 36
5

Map the array to an array of object by apply Object.fromEntries() to each sub array:

const input = [[["firstName","Joe"],["lastName","Blow"],["age",42],["role","clerk"]],[["firstName","Mary"],["lastName","Jenkins"],["age",36],["role","manager"]]];

const result = input.map(Object.fromEntries);

console.log(result);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • 1
    Great one line answer. ikhvjs was a little faster so I'm going to have to give it to them. Thanks for your response. – Funlamb Sep 07 '21 at 18:31
1

You can achieve the result using for..of loop

var input = [
  [
    ["firstName", "Joe"],
    ["lastName", "Blow"],
    ["age", 42],
    ["role", "clerk"],
  ],
  [
    ["firstName", "Mary"],
    ["lastName", "Jenkins"],
    ["age", 36],
    ["role", "manager"],
  ],
];

const result = [];
for (let arr1 of input) {
  let temp = {};
  for (let [k, v] of arr1) {
    temp[k] = v;
  }
  result.push(temp);
}

console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42
  • Why does the for..of loop work but not the loop I created work? I'm having a hard time seeing what I did wrong with my loop. I see that yours is a lot better. I'm just asking so I can understand better. – Funlamb Sep 07 '21 at 18:30