javascript - 使用 javascript 将多个对象结果集的数组合并到单个数组中

标签 javascript arrays angularjs object merge

我有一个包含多个对象的数组结果,我需要使用 java 脚本将此结果集合并到单个数组中。我尝试过许多 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...
  }

]

最佳答案

您可以使用 reduceconcat 快速转换它:

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');
<h1>Before</h1>
<pre id="before"></pre>
<h1>After</h1>
<pre id="results"></pre>

关于javascript - 使用 javascript 将多个对象结果集的数组合并到单个数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34879433/

相关文章:

javascript - 固定为6轮,为什么不是5轮?

JavaScript 日期格式忽略时区

angularjs - 如何在同一页面中运行两个单独的 Angular js 应用程序

javascript - 无法在 Angular 水疗中心加载不同的页面

javascript - 无法读取 null : Cant find source of it 的属性 'removeAttribute'

java - 尝试将数组的元素设置为父类(super class)对象时出现 ArrayStoreException

java - 数组中的三个最大值

arrays - 当我 grep 数组时,如何获取元素的索引?

angularjs - 从一组编辑器中删除 tinymce 4 编辑器

javascript - 即使函数不返回任何内容,onBeforeUnload 也会创建提示