简体   繁体   中英

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

]

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> 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM