Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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!

share|improve this question
1  
You are looking for map() -> reduce() – Nenad Vracar 20 hours ago
1  
may be lodash _flatMap will help you – Samundra KC 20 hours ago
    
Ahh! I thought you were saying only use map, but using map then reduce will definitely do it! Thanks! – Jon M. Langel 20 hours ago
up vote 4 down vote accepted

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; }

share|improve this answer

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);

share|improve this answer
    
This is another great way to accomplish this and it still incorporates the reduce that I was originally stuck on using. Thanks! – Jon M. Langel 20 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.