-1

I cannot wrap my head around this problem. Here is how my array is logically built:

array1 = [ [array2], [array3], [array4] ... [array17] ]
array2 = [ ['obj1'], ['obj2'], ['obj3'] ... ['obj30'] ]
...
...
obj1 = ({prop1 : 'string1'}, { prop2 : 'string2'}, {prop3 : 'string3'} ... {prop30 : 'string30'}) 
obj2 = ({prop1 : 'string1'}, { prop2 : 'string2'}, {prop3 : 'string3'} ... {prop30 : 'string30'}) 
obj3 = ({prop1 : 'string1'}, { prop2 : 'string2'}, {prop3 : 'string3'} ... {prop30 : 'string30'}) 
...
...
obj30 = ({prop1 : 'string1'}, { prop2 : 'string2'}, {prop3 : 'string3'} ... {prop30 : 'string30'})

I want to flatten it into a JSON object like :

{"array1":[
    { "array2":[
        {"obj1":[
            {"prop1" : "string1",
             "prop2" : "string2"
            }
        }]
    }]
]}

Here is what I came up with:

for (i=0; i < array1; i++) {

  var count = 0

  while (count < array1[0].length) {
    var jsonObj = {
      array[i] : {
        array[count] : {
          obj[count] : {
            'prop1' : 'string1'
          }
        }
      }
    }
    count++;
  }
}
CiscoKidx
  • 922
  • 8
  • 29
  • Please include the code / a [mcve] in the question itself. Let us know what the specific problem is / where you got stuck. Makes it easier for us to help you, and will keep the question more relevant to future visitors. – Jeroen Aug 02 '16 at 05:21
  • Your code is missing at least one `}`. Please fix it where it is wrong. – acdcjunior Aug 02 '16 at 05:24
  • perhaps worth having a look at underscorejs's flatten function http://underscorejs.org/#flatten – haxxxton Aug 02 '16 at 05:26
  • First please implement a fiddle or plunker. Am assuming that You can play very with objects and arrays while using [1]: http://underscorejs.org/ [2]: http://lodash.com/ – The Mechanic Aug 02 '16 at 05:31
  • If you're just trying to flatten your array of arrays into a single array, you can read this [Merge/flatten an array of arrays in JavaScript?](http://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays-in-javascript) – jfriend00 Aug 02 '16 at 06:16
  • Sorry for the confusing question.. I attempting simplifying and clarifying. Please see above. – CiscoKidx Aug 03 '16 at 00:44

2 Answers2

0

You could either push all responseData.res while looping over responses

secondRes.push.apply(secondRes, responseData.res)

Or flatten firstRes afterwards using concat

var secondRes = firstRes.concat.apply([], firstRes);
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98
0

So, the question is a bit confusing. But if you want to create an array of arrays out from an array of objects, this is how you do it.

secondArray=[];

firstArray.map((object, key) => {

 let innerArray = [];

 for (let prop in object){

  innerArray.push(object[prop])

 }

 secondArray.push(newArray);

});
Giovanni Lobitos
  • 852
  • 7
  • 19