我有一个包含多个对象的数组结果,我需要使用java-script将此结果集合并到单个数组中。我尝试过很多JavaScript函数但没有成功。此问题也在使用array.push
后生成了响应。
[
[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...
}]
]
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);
}
[
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...
}
]
答案 0 :(得分:2)
您可以使用reduce
和concat
快速转换:
arr.reduce(function(a, b){ return a.concat(b); });
示例:
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');
&#13;
<h1>Before</h1>
<pre id="before"></pre>
<h1>After</h1>
<pre id="results"></pre>
&#13;
答案 1 :(得分:0)
尝试:
var result = [];
for (var i=0; i<Response.length; ++i) {
for (var j=0; j<Response[i].length; ++j) {
result.push(Response[i][j]);
}
}