JavaScript Flatten and merge array of array of objects in Javascript flatten concatenate array

Here is my input:

var toto=[
    [
        { "a": "24" },
        { "a": "23.9"},
        { "a": "NaN"},
        { "a": "3" }
    ],
    [
        {"b": "19"},
        {"b": "20"},
        {"b": "NaN"},
        {"b": "3" }
    ],
    [
        {"c": "27"},
        {"c": "28"},
        {"c": "NaN"},
        {"c": "3" }
    ]
];

All arrays of objects are guaranteed to contain the same number of objects.

I would like to obtain in output:

var out =  [
        { "a": "24", "b": "19", "c":"27" },
        { "a": "23.9", "b": "20", "c":"28"},
        { "a": "NaN", "b": "NaN", "c":"NaN"},
        { "a": "3", "b": "3", "c": "3"}
    ]

Which is, for each inner array, take the Nth element and merge it into an object, and push it into a result array.

I have a "braindead" solution which is iterating on the first sub-array and then iterating on the other arrays:

var out = [];
$(toto[0]).each(function(i, item) {
   var o = {};
   $(toto).each(function( j, array) {
    var item = array[i];
    o[Object.keys(item)[0]] = Object.values(item)[0];
   });
   out.push(o);
});

It works, but it is kind of hideous, and I would like to know if there's a solution using functional methods like map and reduce.

Do you guys know?

Answer:1

I'm trying to load in audiosprite in my React app which is running on a Webpack server and I'm getting the following error: Module not found: 'child_process' in C:\Users\BenLi\Desktop\development ...

I'm trying to load in audiosprite in my React app which is running on a Webpack server and I'm getting the following error: Module not found: 'child_process' in C:\Users\BenLi\Desktop\development ...

I want to remove (or make effectively hidden) the first vertical line in the grid for an nvd3 chart. I thought it was a problem with my chart, but after testing it, I realized it seems to be a more ...

I want to remove (or make effectively hidden) the first vertical line in the grid for an nvd3 chart. I thought it was a problem with my chart, but after testing it, I realized it seems to be a more ...

I'm having trouble understanding exactly how to use older javascript libraries within newer ES6 projects. I'm looking at a React project that's been compiled with webpack, written with ES6 and ...

I'm having trouble understanding exactly how to use older javascript libraries within newer ES6 projects. I'm looking at a React project that's been compiled with webpack, written with ES6 and ...

  1. external javascript libraries
  2. include external javascript libraries in an angular typescript project
  3. include external javascript libraries in an angular
  4. angularjs external javascript libraries
  5. using external javascript libraries
  6. include external javascript libraries in an angular 4
  7. include external javascript libraries in an angular 6
  8. include external javascript libraries in an angular 7 typescript project
  9. lazy loading external javascript libraries in angular
  10. include external javascript libraries in an angular 2 typescript project
  11. include external javascript libraries in

I'm writing a chrome extension that will change the color of certain words on the screen. I am having trouble finding the text which meets my needs and then accurately wrapping it in a span. ...

I'm writing a chrome extension that will change the color of certain words on the screen. I am having trouble finding the text which meets my needs and then accurately wrapping it in a span. ...

  1. wrap text span
  2. wrap text span css
  3. wrap text span html
  4. text wrap span tag
  5. wrap text inside span css
  6. wrap text inside span
  7. wrap text in span jquery
  8. wrap text in span javascript
  9. wrap text in span tag html
  10. wrap text in span using css
  11. wrap text within span
  12. wrap text in span js
  13. wrap text around span
  14. wrap text in span tag jquery
  15. wrap text inside span tag
  16. word wrap text span