S Q
S Q

Reputation: 129

Turn nested arrays into objects

I have been unable to figure out how to turn a nested array such as:

var array = [
    [['firstName', 'Henry'], ['codeName', 'Etta'], ['email', '[email protected]'], ['weight', 180], ['occupation', 'repo']],
    [['firstName', 'Bruce'], ['codeName', 'DK'], ['email', '[email protected]'],
 ['weight', 200], ['occupation', 'enforcement']]
];

into an object such as

var array = [
    {firstName: 'Henry', codeName: 'Etta', email: '[email protected]', weight: 180, occupation: 'repo'},
    {firstName: 'Bruce', codeName: 'DK', email: '[email protected]', weight: 200, occupation: 'enforcement'}
];

Below is what I've come up with so far, but it is clearly not producing the results I need.

function arrIntoObject(array) {
  var obj = {};

  array.map(function(a) {
    a.map(function(e) {
      obj[e[0]] = e[1];  
    });
  });
  return obj;
}

This seems like a question that would have been asked by now, but after hours I haven't been able to find a similar question, so I would appreciate any help or guidance here. Thanks!

Upvotes: 3

Views: 790

Answers (6)

adeneo
adeneo

Reputation: 318212

You could just reduce the arrays into an object

var array = [
  [['firstName', 'Henry'],['codeName', 'Etta'],['email', '[email protected]'],['weight', 180],['occupation', 'repo']],
  [['firstName', 'Bruce'],['codeName', 'DK'],['email', '[email protected]'],['weight', 200],['occupation', 'enforcement']]
];

var obj = array.map( arr => arr.reduce( (acc, curr) => { 
    acc[ curr[0] ] = curr[1]; return acc;
}, {}));


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

Upvotes: 2

Ammar
Ammar

Reputation: 820

array.map(val => val.reduce(function(obj, prop) {
  obj[prop[0]] = prop[1];
  return obj;
}, {}));

Upvotes: 0

TheGr8_Nik
TheGr8_Nik

Reputation: 3200

You can use the reduce method, it permits to reduce an array to a single variable, in this case an object. For more informations give a look to MDN

var initialArray = [
    [['firstName', 'Henry'], ['codeName', 'Etta'], ['email', '[email protected]'], ['weight', 180], ['occupation', 'repo']],
    [['firstName', 'Bruce'], ['codeName', 'DK'], ['email', '[email protected]'],
 ['weight', 200], ['occupation', 'enforcement']]
];

function arrayToObject(a) {
  return a.reduce(function (obj, keyValue) {
    obj[keyValue[0]] = keyValue[1];
    return obj;
  }, {});
}

var finalArray = initialArray.map(arrayToObject);

console.log(finalArray);

Upvotes: 1

Nenad Vracar
Nenad Vracar

Reputation: 122047

You can use map() with spread syntax ... and Object.assign()

var array = [[['firstName', 'Henry'], ['codeName', 'Etta'], ['email', '[email protected]'], ['weight', 180], ['occupation', 'repo']],[['firstName', 'Bruce'], ['codeName', 'DK'], ['email', '[email protected]'],['weight', 200], ['occupation', 'enforcement']]];

var result = array.map(e => Object.assign({}, ...e.map(([k, v]) => ({[k]: v}))))
console.log(result)

You can also use map() and then reduce() with Object.assign()

var array = [[['firstName', 'Henry'], ['codeName', 'Etta'], ['email', '[email protected]'], ['weight', 180], ['occupation', 'repo']],[['firstName', 'Bruce'], ['codeName', 'DK'], ['email', '[email protected]'],['weight', 200], ['occupation', 'enforcement']]];

var result = array.map(e => e.reduce((r, [k, v]) => Object.assign(r, {[k]: v}),{}))
console.log(result)

Upvotes: 1

Thusitha
Thusitha

Reputation: 3511

Hope this simple logic might helps.

var array = [
  [
    ['firstName', 'Henry'],
    ['codeName', 'Etta'],
    ['email', '[email protected]'],
    ['weight', 180],
    ['occupation', 'repo']
  ],
  [
    ['firstName', 'Bruce'],
    ['codeName', 'DK'],
    ['email', '[email protected]'],
    ['weight', 200],
    ['occupation', 'enforcement']
  ]
];

var output = [];
array.forEach(function(arr1) {
  output.push({});
  arr1.forEach(function(arr2) {
    output[output.length - 1][arr2[0]] = arr2[1];
  })
});

console.log(output);

Upvotes: 0

mhodges
mhodges

Reputation: 11116

You can use a combination of .map() with .reduce(), like so:

var array = [
    [['firstName', 'Henry'], ['codeName', 'Etta'], ['email', '[email protected]'], ['weight', 180], ['occupation', 'repo']],
    [['firstName', 'Bruce'], ['codeName', 'DK'], ['email', '[email protected]'],
 ['weight', 200], ['occupation', 'enforcement']]
];

var objs = array.map(function (arr) {
  return arr.reduce(function (res, curr) {
    var [key, value] = curr;
    res[key] = value;
    return res;
  }, {});
});

console.log(objs);

Upvotes: 2

Related Questions