Jon Langel
Jon Langel

Reputation: 177

Convert multi-depth array of arrays object to array of objects

I'm having a little trouble converting an array of arrays-like object to an array of objects. I am using the reduce method, and it is correctly converting the array data to an object for the first set of arrays but the second time through, it correctly sets the data as key:value pairs but does not place it in the object which I want it to be in.

Here is the array and the function:

var array = [
              [
                ['Name', 'Arusha'], ['Species', 'Arabica'], ['Region', 'Mount Meru in Tanzania, and Papua New Guinea'], ['Comments', 'Typica variety or a French Mission']
              ],
              [
                ['Name', 'Catimor'], ['Species', 'Interspecific hybrid'], ['Region', 'Latin America'], ['Comments', 'This is cross between Timor coffee and Caturra coffee. It was created in Portugal in 1959']
              ]
            ];

function convertArrayToObject(array) {
    var arr = [];

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

My expected output is suppose to look something like this:

[
{ Name: 'Arusha', Species: 'Arabica', Region: 'Mount Meru in Tanzania, and Papua New Guinea', Comments: 'Typica variety or a French Mission' },
{ Name: 'Catimor', Species: 'Interspecific hybrid', Region: 'Latin America', Comments: 'This is cross between Timor coffee and Caturra coffee. It was created in Portugal in 1959' }
]

This is what I'm currently returning:

 [ { Name: 'Arusha', Species: 'Arabica', Region: 'Mount Meru in Tanzania, and Papua New Guinea', Comments: 'Typica variety or a French Mission' },
[Circular],
Name: 'Catimor',
Species: 'Interspecific hybrid',
Region: 'Latin America',
Comments: 'This is cross between Timor coffee and Caturra coffee. It was created in Portugal in 1959' ]

Any help would be greatly appreciated!

Upvotes: 1

Views: 95

Answers (2)

Ori Drori
Ori Drori

Reputation: 192857

Use Array#map to iterate the array, and Array#reduce each sub array to an object:

array = [[['Name', 'Arusha'], ['Species', 'Arabica'], ['Region', 'Mount Meru in Tanzania, and Papua New Guinea'], ['Comments', 'Typica variety or a French Mission']], [['Name', 'Catimor'], ['Species', 'Interspecific hybrid'], ['Region', 'Latin America'], ['Comments', 'This is cross between Timor coffee and Caturra coffee. It was created in Portugal in 1959']]];

var result = array.map(function(arr) {
  return arr.reduce(function(obj, tuple) {
    obj[tuple[0]] = tuple[1];
    
    return obj;
  }, {});
});

console.log(result);

Upvotes: 3

Nina Scholz
Nina Scholz

Reputation: 386796

You could use Array#map and return a new object for each element of the outer array. Inside, you could iterate all items and build new properties.

var array = [[['Name', 'Arusha'], ['Species', 'Arabica'], ['Region', 'Mount Meru in Tanzania, and Papua New Guinea'], ['Comments', 'Typica variety or a French Mission']], [['Name', 'Catimor'], ['Species', 'Interspecific hybrid'], ['Region', 'Latin America'], ['Comments', 'This is cross between Timor coffee and Caturra coffee. It was created in Portugal in 1959']]],
    result = array.map(function (a) {
        var o = {};
        a.forEach(function (b) {
            o[b[0]] = b[1];
        });
        return o;
    });

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

Upvotes: 4

Related Questions