LoginSignup
1
1

More than 3 years have passed since last update.

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

Posted at

配列っぽいオブジェクト

{
  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) {
  const newArray = Object.entries(listObject).map(([key, value]) => ({
    ...value,
  }))
  return newArray.sort((a, b) => (a.sort_id > b.sort_id ? 1 : -1))
},

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

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1