magicsword
magicsword

Reputation: 1289

Array of Arrays to list of dictionaries

I'm passing this to the function Data(array):

[
[
    ['firstName', 'Joe'], ['lastName', 'low']
  ],
[
    ['firstName', 'Steph'], ['lastName', 'Roberts']
]
]

would like to output:

[
{firstName: 'Joe', lastName: 'low'},
{firstName: 'Steph', lastName: 'Roberts'}
]

This is what I have:

function Data(array) {
var dict ={};
for(var j =0; j<=array.length-1; j++){
  for(var i=0; i<=array[0].length-1; i++){
    dict[array[j][i][0]] = array[j][i][1];
  }
}
return dict
}

But the output is not correct:

{ firstName: 'Steph',
lastName: 'Roberts',
}

Upvotes: 1

Views: 153

Answers (5)

Vinod Kumawat
Vinod Kumawat

Reputation: 741

You should declare one array variable for desired output instead of object . Object will create your JSON but you want results as JSON object in array formats

function Data(array)  { 
 var res=[]; 
var dict ={}; 
 for(var j =0; j<=array.length-1; j++) 
{  
for(var i=0;i<=array[0].length-1; i++) 
{  
dict[array[j][i][0]] = array[j][i][1]; 
}
res.push(dict); 
 }  
return res;  
}

Upvotes: 0

Stephan
Stephan

Reputation: 2115

AvcS's answer contains the reason why your code is failing. Nina's solution is perfect if you can utilize a transpiler like Babel since browser support for destructuring assignment looks quite bad.

As an alternative you could also use map and reduce, which maybe simplifies the code needed:

let inp = [
  [['firstName', 'Joe'], ['lastName', 'low']],
  [['firstName', 'Steph'], ['lastName', 'Roberts']]
];

let result = inp.map(
  entry => entry.reduce(
    (obj, keyvalue) => {
      obj[keyvalue[0]] = keyvalue[1];
      return obj;
    }, {})
);

console.log(result);

Don't forget to maybe add polyfills for map and replace and maybe replacing the lets and arrow functions.

Upvotes: 1

AvcS
AvcS

Reputation: 2323

You are updating the same object on every iteration, instead you should create new object on every iteration and push it to an array

function Data(array) {
    var dict = [];
    for(var j =0; j<=array.length-1; j++) {
        dict[j] = {};
        for(var i=0; i<=array[0].length-1; i++) {
            dict[j][array[j][i][0]] = array[j][i][1];
        }
    }
    return dict;
}

console.log(Data([
    [
        ['firstName', 'Joe'], ['lastName', 'low']
    ],
    [
        ['firstName', 'Steph'], ['lastName', 'Roberts']
    ]
]));

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386560

You could take an array as result and take a single dictinary for each outer item.

function getArrayOfObjects(array) {
    var result = [],
        dict,
        i, j;
      
    for (j = 0; j < array.length; j++) {
        dict = {};
        for (i = 0; i < array[0].length; i++) {
            dict[array[j][i][0]] = array[j][i][1];
        }
        result.push(dict);
    }
    return result;
}

var array = [[['firstName', 'Joe'], ['lastName', 'low']], [['firstName', 'Steph'], ['lastName', 'Roberts']]];

console.log(getArrayOfObjects(array));

ES6 with

function getArrayOfObjects(array) {
    return array.map(a => Object.assign(...a.map(([k, v]) => ({ [k]: v }))))
}

var array = [[['firstName', 'Joe'], ['lastName', 'low']], [['firstName', 'Steph'], ['lastName', 'Roberts']]];

console.log(getArrayOfObjects(array));

Upvotes: 1

guest271314
guest271314

Reputation: 1

Substitute array[j] for array[0] at i<=array[0].length-1;, set an array as a property of dict object

function Data(array) {
  var dict = {
    array: []
  };
  for (var j = 0; j <= array.length - 1; j++) {
    let o = {};
    for (var i = 0; i <= array[j].length - 1; i++) {
      o[[array[j][i][0]]] = array[j][i][1];
    }
    dict.array.push(o)
  }
  return dict
}

console.log(Data([
  [
    ['firstName', 'Joe'],
    ['lastName', 'low']
  ],
  [
    ['firstName', 'Steph'],
    ['lastName', 'Roberts']
  ]
]).array);

Upvotes: 1

Related Questions