angular keyvalue pipe sort properties / iterate in order

前端 未结 11 953
有刺的猬
有刺的猬 2020-11-29 21:01

When using the Angular keyvalue pipe to iterate over an object\'s properties as follows:

11条回答
  •  情歌与酒
    2020-11-29 21:24

    I think this is a cleaner solution. Uncomment the return statement that needs to give the right order:

    interface KeyValue {
      key: K,
      value: V
    }
    
    // Order by descending property key
      keyOrder = (aA: KeyValue, bB: KeyValue): number => {
        const a = aA.value.index;
        const b = bB.value.index;
        //return a > b ? 1 : (b > a ? -1 : 0); // ASCENDING
        return a > b ? -1 : (b > a ? 1 : 0); // DESCENDING
      }
    

    USE CASE

    {{ x.key }} indexes: {{ x.value.index }}

提交回复
热议问题