Query Master
Query Master

Reputation: 7097

merge array of multiple object result set in single array using javascript

I have a result of array which contain multiple objects and i need to merge this result set in single array using java-script. I have tried with many of JavaScript functions but no success. Also this one have response generated after using array.push.

Response

[

  [Object {
      category_of_student = "Freshman", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
    },
    Object {
      category_of_student = "Freshman", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
    },
    Object {
      category_of_student = "Ophomore", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
    },
    26 more...
  ],

  [Object {
    category_of_student = "Junior", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
  }]

]

Code

var yourCat = item.category_of_student;

var optionVal = [];
for (i = 0; i < yourCat.length; i++) {

  var res = ($filter('filter')(item, {
    category_of_student: yourCat[i]
  }));
  optionVal.push(res);

}

Require Result

[

  Object {
    category_of_student = "Freshman", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
  },
  Object {
    category_of_student = "Freshman", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
  },
  Object {
    category_of_student = "Ophomore", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
  },
  26 more...,

  Object {
    category_of_student = "Junior", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
  }

]

Upvotes: 0

Views: 159

Answers (2)

MinusFour
MinusFour

Reputation: 14423

You can quickly transform this with reduce and concat:

arr.reduce(function(a, b){  return a.concat(b); });

Example:

var arr = [
  [{
    category_of_student : "Freshman",
    your_school : "(BA Bs)Bachelor of Scien...Business Administration",
    college : "Business of Unit"
  },
  {
    category_of_student : "Freshman",
    your_school : "(BA Bs)Bachelor of Scien...Business Administration",
    college : "Business of Unit"
  },
  {
    category_of_student : "Freshman",
    your_school : "(BA Bs)Bachelor of Scien...Business Administration",
    college : "Business of Unit"
  }],
  [{
    category_of_student : "Junior",
    your_school : "(BA Bs)Bachelor of Scien...Business Administration",
    college : "Business of Unit"
  }]
]

var transformed = arr.reduce(function(a, b){ return a.concat(b); });

before.innerHTML = JSON.stringify(arr, null, '\t');
results.innerHTML = JSON.stringify(transformed, null, '\t');
<h1>Before</h1>
<pre id="before"></pre>
<h1>After</h1>
<pre id="results"></pre>

Upvotes: 2

jcubic
jcubic

Reputation: 66478

Try:

var result = [];
for (var i=0; i<Response.length; ++i) {
  for (var j=0; j<Response[i].length; ++j) {
    result.push(Response[i][j]);
  }
}

Upvotes: 0

Related Questions