javascipt的扁平化对象的数组的数组 [英] Javascipt flattening an array of arrays of objects

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

问题描述

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

  [object1,Object2的],[object1],[object1,Object2的,object3]

下面是登录到控制台对象的screenhot。

什么是最好的办法,以扁平化这一点,所以它只是一个对象数组?

我已经没有运气尝试这样做:

 的console.log(searchData);
  变种M = [] .concat.apply([],searchData);
的console.log(米);

searchData注销上面的截图但是,M注销[]

下面是searchData的实际内容:

  [[{_ ID:55064111d06b96d974937a6f,称号:通用名称,短名称:通用标题,内容:< P&GT ;该治疗中心提供给个人和家庭实际的,社会的和精神上的支持服务包括但不限于:食品和衣物,职业技能培训和求职援助,汽车修理(仅星期六),辅导,财务辅导,辅导,祷告,生活技能培训,以及有关当地社区服务的有用信息和LT; / p>< p>保持与我们联系:< / p>中,__ v:0},{_ ID: 5508e1405c621d4aad2d2969,称号:考英语,短名称:测试页,内容:< H2>英语测试与LT; / H2>中,__ v:0}],[{ _id:550b336f33a326aaee84f883,短名称:OK-URL,称号:现在英语,内容:< p> okokko< / p>中,类别:交通运输 __ v:0}]


解决方案

您可以使用的 Array.concat 像波纹管: -

 变种改编= [['object1','Object2的'],['object1'],['object1','Object2的','object3']];
VAR扁平= [] .concat.apply([],ARR);

扁平将是您预期的数组。

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?

I've tried this with no luck:

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

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

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

解决方案

You can use Array.concat like bellow:-

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

flattened will be your expected array.

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

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