2017-05-12 35 views
0

我有一系列項目matchingItemsCurrentRow。它有IBwFormSectionItem其中有一個屬性runCount項目。我想獲得具有最大值runCount的數組項目。我怎麼才能得到它?如何獲得陣列項目中具有最大屬性值的陣列項目

IBwFormSectionItem的類型定義如下:

export interface IBwFormSectionItem { 
    meta?: IBwFormItemMetadata[], 
    font?: IBwFormFont, 
    col?: number, 
    row?: number, 
    colSpan?: number, 
    rowSpan?: number, 
    text?: string, 
    runs?: boolean, 
    runCount?: number, 
    height?: number, 
    width?: number 
} 
+2

請發佈您使用JSON格式的數據以及預期的輸出。 – Bergi

+0

此問題已被詢問和回答多次。大多數解決方案都使用'reduce'來遍歷項目。 –

+0

沒有得到笏你試圖做。如果你想從數組中獲得最大值嘗試這個代碼... Math.max.apply(this,[1,2,3,4]); – subbu

回答

0

我就是這麼做的指torazaburo的評論。

maxRunCountItem = matchingItemsCurrentRow.reduce((item1, item2) => {       
    return item1.runCount > item2.runCount ? item1 : item2; 
});