0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Javascript】オブジェクト配列で指定キーのmin, maxを取得する

Last updated at Posted at 2022-12-16

reduceを使います。
prevとcurrentをキーで比較し、オブジェクトをreturnします。

const data = [
  { name: "aaa", score: 90 },
  { name: "bbb", score: 30 },
  { name: "ccc", score: 20 },
  { name: "ddd", score: 70 },
  { name: "eee", score: 50 },
];

// scoreが最大を取得
const highest = data.reduce((prev, current) => {
  if (prev.score > current.score) {
    return prev;
  } else {
    return current;
  }
});

console.log(highest);

結果

{ name: 'aaa', score: 90 }
0
0
1

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?