刚刚开始学习 Javascript 和 Angular。 我有一个数组中的数组。
这是我的数组结构:
[[对象1,对象2],[对象3,对象4]];
我想从对象 2 和对象 4 中获取最高值。
当我只有一个数组时,我使用了
Math.max.apply(Math, array.map(function(o){return o.collectionID;}));
是否可能以及如何从数组中的数组中检索最高值?
最佳答案
您可以通过Array#reduce
收集所有id
对于任何深度数组。
然后仅在设置的地方获取collectionID
。
var array = [[{}, { collectionID: 1 }], [{}, { collectionID: 2 }], [{}, { collectionID: 3 }], [{ collectionID: 42 }, { collectionID: 4 }]],
max = Math.max.apply(Math, array.reduce(function iter(r, a) {
return r.concat(Array.isArray(a) ? a.reduce(iter, []) : 'collectionID' in a ? a.collectionID : []);
}, []));
console.log(max);
关于javascript - 获取数组内数组的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42997528/