Quantcast
Viewing all articles
Browse latest Browse all 757

配列っぽいオブジェクトを任意のキーの配列に変換

配列っぽいオブジェクト

{
  8347923: {
    name: 'foo',
    age: '43',
    sort_id: 4,
  },
  349: {
    name: 'bar',
    age: '24',
    sort_id: 2,
  },
  982123: {
    name: 'baz',
    age: '31',
    sort_id: 1,
  },
  23: {
    name: 'qux',
    age: '14',
    sort_id: 3,
  },
  ...
}

sort_id順の配列に変換する。

sortedArray(listObject){constnewArray=Object.entries(listObject).map(([key,value])=>({...value,}))returnnewArray.sort((a,b)=>(a.sort_id>b.sort_id?1:-1))},

オブジェクトを適当に配列に変換して評価。


Viewing all articles
Browse latest Browse all 757

Trending Articles