从嵌套的对象数组优化数组构建
optimize array building from nested arrays of objects
我想在Javascript中优化从对象数组中提取属性值,每个对象包含其他嵌套的对象数组。
我不太确定如何用语言来解释它,所以这里有一些代码来解释我正在尝试优化的内容:
// test case
var first = [
{ second: [ { id: 1}, { id: 2}, { id: 3} ] },
{ second: [ { id: 4}, { id: 5}, { id: 6} ] },
{ second: [ { id: 7}, { id: 8}, { id: 9} ] },
{ second: [ { id: 10}, { id: 11}, { id: 12} ] }
];
// where the id values will be stored
var arrIDs = [];
// extracting the id values
for (var i=0; i<first.length; i++){
for (var j=0; j<first[j].second.length; j++){
// I want to avoid all these push() calls
arrIDs.push(first[j].second[j].id);
}
}
这就是我想要达到的最终结果:
arrIDs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
我可能需要最多3或4个嵌套关卡,但如果无法优化更多,我可能会使用不同的结构将其减少到2个关卡。
我最想优化的实际上是for循环中的所有Array().push()调用。
有人知道这样做的好方法吗?
编辑我忘了提到,我需要在IE8是我们拥有的最佳选择的环境中使用它。
对于您的数据结构,您可以使用reduce()
和map()
。
var first = [
{ second: [ { id: 1}, { id: 2}, { id: 3} ] },
{ second: [ { id: 4}, { id: 5}, { id: 6} ] },
{ second: [ { id: 7}, { id: 6}, { id: 9} ] },
{ second: [ { id: 10}, { id: 11}, { id: 12} ] }
];
var result = first.reduce(function(r, o) {
r = r.concat(o.second.map(function(e) {
return e.id;
}))
return r;
}, []);
console.log(result)
ES6版本
var first = [
{ second: [ { id: 1}, { id: 2}, { id: 3} ] },
{ second: [ { id: 4}, { id: 5}, { id: 6} ] },
{ second: [ { id: 7}, { id: 6}, { id: 9} ] },
{ second: [ { id: 10}, { id: 11}, { id: 12} ] }
];
var result = first.reduce((r, o) => r.concat(o.second.map(e => e.id)) , []);
console.log(result)
您可以使用这个递归ES6函数。它适用于任何级别,您可以向它传递一个数组,普通对象或其他任何东西(在后一种情况下,您将得到一个空数组作为结果):
function getAll(obj, key) {
return obj !== Object(obj) ? []
: Object.keys(obj).reduce ( (acc, k) =>
acc.concat(k == key ? obj[k] : getAll(obj[k], key)), [] );
}
// sample data with different levels and mix of array / object alterations
var first = [
{ a: 2, id: 1}, { second: [ { id: 2}, { id: 3} ] },
{ second: [ { id: 4}, { id: 5}, { id: 6} ] },
{ second: [ { id: 7}, { third: {id: 6}}, { id: 9} ] },
{ second: [ { id: 10}, { id: 11}, { id: 12} ] }
];
var result = getAll(first, 'id');
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
IE8兼容性:
function getAll(obj, key) {
if (typeof obj != 'object' || obj == null) return [];
var acc = [];
for (var k in obj) {
acc = acc.concat( k == key ? obj[k] : getAll(obj[k], key) );
}
return acc;
}
// sample data with different levels and mix of array / object alterations
var first = [
{ a: 2, id: 1}, { second: [ { id: 2}, { id: 3} ] },
{ second: [ { id: 4}, { id: 5}, { id: 6} ] },
{ second: [ { id: 7}, { third: {id: 6}}, { id: 9} ] },
{ second: [ { id: 10}, { id: 11}, { id: 12} ] }
];
var result = getAll(first, 'id');
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
您可以使用迭代和递归提案。此方法适用于任何级别。
function getValues(o, key) {
return o !== null && typeof o === 'object' && Object.keys(o).reduce(function (r, k) {
return r.concat(k === key ? o[k] : getValues(o[k], key));
}, []) || [];
}
var first = [{ second: [{ id: 1 }, { id: 2 }, { id: 3 }] }, { second: [{ id: 4 }, { id: 5 }, { id: 6 }] }, { second: [{ id: 7 }, { id: 6 }, { id: 9 }] }, { second: [{ id: 10 }, { id: 11 }, { id: 12 }] }];
console.log(getValues(first, 'id'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
相关文章:
- 如何在映射数组中添加换行符
- javascript结合了数组和字典
- 需要帮助设置json数组
- 不能从angular2中的子组件指定父组件中的数组
- JavaScript数组优化以提高性能
- 与哈希表相比,通过循环访问数组中对象的Javascript优化
- javascript if语句优化数组长度标志
- 我如何优化一个长数组在javascript
- 从嵌套的对象数组优化数组构建
- 本机map, filter等方法是否优化为尽可能在单个中间数组上操作
- 尝试在循环中使用固定数组点进行catch优化
- Javascript数组优化
- 我如何在JavaScript数组中优化我的代码
- JQuery while-loop在使用inArray比较优化数组内容时变得无响应
- 优化的纯JavaScript意味着迭代静态HTML表,根据其行/列创建对象数组
- 用Javascript优化数组中的自定义对象属性
- JavaScript引擎是否对多维数组执行任何优化
- 优化Javascript方法来填充或清空数组
- JS -搜索数组对象-优化
- 优化javascript代码在数组中搜索匹配