javascript - 有没有办法从数组中取出对象并放入新数组中?
问题描述
鉴于:
myObjArray = [ [ [ [Object] ] ], [ [ [Object] ] ], [ [ [Object] ] ] ]
期望的结果
[{Object},{Object},{Object}]
那么有没有办法将 [Object(s)] 从数组中拉出并放入一个新数组中?
解决方案
array.prototype.map
与从嵌套数组中检索对象的递归函数一起使用:
var arr = [ [ [ [ { prop: 'val1' }] ] ], [ [ [{ prop: 'val2' }] ] ], [ [ [{ prop: 'val3' }] ] ] ];
var res = arr.map(getObject);
function getObject(o) {
return Array.isArray(o) ? getObject(o[0]) : o;
}
console.log(res);
推荐阅读
- html - 溢出-y:滚动不起作用,文本超出 div
- android - 如何获取文本中的 PasswordProtection?
- java - 使用 REST 在 JSON 中定位消息成功
- javascript - 数量未在会话中更新
- usb - 处理 OUT 报告端点 (USB HID)
- ios - textView 旋转错误
- python - python包下载失败
- jenkins - 在 Jenkins Build 运行时提供对日志中工作区文件的访问权限
- javascript - 为什么highcharts页面不显示任何东西?
- javascript - Nightsbridge 预订日历,提交表单后将输入值发送到重定向页面