JavaScript展平对象数组的数组 [英] JavaScript flattening an array of arrays of objects

查看:165
本文介绍了JavaScript展平对象数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个数组的数组,每个数组包含几个对象,类似于此。

I have an array which contains several arrays, each containing several objects, similar to this.

[[object1, object2],[object1],[object1,object2,object3]]

这是对象的屏幕截图登录到控制台。

Here is a screenhot of the object logged to the console.

什么是最好的把它弄平的方法所以它只是一个对象数组?

What would be the best approach to flattening this out so it just an array of objects?

我试过这个没有运气:

console.log(searchData);  
  var m = [].concat.apply([],searchData);    
console.log(m);

searchData注销上面的屏幕截图,但是m登出[]

searchData logs out the screenshot above, but m logs out [ ]

以下是searchData的实际内容:

Here is the actual contents of searchData:

[[{"_id":"55064111d06b96d974937a6f","title":"Generic Title","shortname":"generic-title","contents":"<p>The Healing Center offers practical, social, and spiritual support to individuals and families. Services include, but are not limited to: food and clothing, job skills training and job search assistance, auto repair (Saturdays only), mentoring, financial counseling, tutoring, prayer, life skills training, and helpful information about local community services.</p><p>Stay in touch with us:</p>","__v":0},{"_id":"5508e1405c621d4aad2d2969","title":"test english","shortname":"test-page","contents":"<h2>English Test</h2>","__v":0}],[{"_id":"550b336f33a326aaee84f883","shortname":"ok-url","title":"now english","contents":"<p>okokko</p>","category":"Transportation","__v":0}]]


推荐答案

你可以用< a href =https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat> Array.concat 如下: -

You can use Array.concat like bellow:-

var arr = [['object1', 'object2'],['object1'],['object1','object2','object3']];
var flattened = [].concat.apply([],arr);

flattened 将是您期望的数组。

这篇关于JavaScript展平对象数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆