剛開始學習Javascript和Angular。 我有一個數組內的數組。從數組中獲取數組的最大值
這是我的陣列結構:
[[object1,Object2的],[object3,物體4]];
我想從對象2獲得最高的價值和目標4
當我只有一個陣列,我用
Math.max.apply(Math, array.map(function(o){return o.collectionID;}));
是否可能,如何從一個數組中檢索最高值在一個數組?
剛開始學習Javascript和Angular。 我有一個數組內的數組。從數組中獲取數組的最大值
這是我的陣列結構:
[[object1,Object2的],[object3,物體4]];
我想從對象2獲得最高的價值和目標4
當我只有一個陣列,我用
Math.max.apply(Math, array.map(function(o){return o.collectionID;}));
是否可能,如何從一個數組中檢索最高值在一個數組?
你可以收集所有id
與Array#reduce
任何深度的陣列。
然後只需要設置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);
這裏是非常簡單而有力的回答[https://stackoverflow.com/a/23397247/3455035]
將其應用到'parentArray [N]'? –
你可以發佈你的數組結構嗎? –