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?

配列による配列の並び替え

Posted at
// 並び替えの基準となる配列
const baseArray: string[] = ["apple", "banana", "cherry", "date"];

// 並び替えたい配列 (基準配列より要素数が少ない)
const targetArray: string[] = ["cherry", "apple"];

// baseArrayの順にtargetArrayを並び替える
const sortedArray = targetArray
  .filter(item => baseArray.includes(item)) // baseArrayに存在しない要素を除外
  .sort((a, b) => baseArray.indexOf(a) - baseArray.indexOf(b)); // baseArrayの順に並び替え

console.log(sortedArray);
0
0
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
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?