Skip to content

Array.prototype.toSorted

方法返回一个新数组,其中的元素根据提供的比较函数排序。它允许您根据需要自定义排序顺序。此方法不会改变原始数组。

js
// toSorted has the same signature as sort, but it creates a new array instead of operating on the array itself.
const arr = [5, 4, 2, 3, 1]
console.log(arr.sort()) // [1, 2, 3, 4, 5]
console.log(arr.toSorted()) // [1, 2, 3, 4, 5]
js
// toSorted also accepts a single optional argument, a comparator function.
// For example, we could use to create a new array in descending order,
const numbers = [10, 5, 2, 7, 3, 9, 1, 6, 4]
const sortedNumbers = numbers.toSorted((a, b) => {
  return b - a
})
console.log(sortedNumbers) // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
js
// Also, note that this can be applied to arrays of objects.
// In such cases, you must provide a comparator function that utilizes the data within the objects since there is no inherent ordering for objects.
const objects = [
  { name: 'John', age: 30 },
  { name: 'Jane', age: 25 },
  { name: 'Bill', age: 40 },
  { name: 'Mary', age: 20 },
]
const sortedObjects = objects.toSorted((a, b) => {
  return a.name.localeCompare(b.name)
})
console.log(sortedObjects)
// [{"name":"Bill","age":40},{"name":"Jane","age":25},{"name":"John","age":30},{"name":"Mary","age":20}]

Array.prototype.toReversed

该方法返回一个新数组,其中元素的顺序相反。它不会改变原始数组。

js
const array = [1, 2, 3]
console.log(array.toReversed()) // [3, 2, 1]
console.log(array) // [1, 2, 3]

Array.prototype.with

with() 方法允许你根据索引修改单个元素,并返回一个新的数组。

js
const array = ['I', 'am', 'the', 'cd']

// Replace the string "cd" with "wd".
const newArray = array.with(3, 'wd')

console.log(newArray) // ['I', 'am', 'the', 'wd']
console.log(array) // ['I', 'am', 'the', 'cd']

Array.prototype.findLast

方法反向迭代数组,并返回满足提供的测试函数的第一个元素的值。否则返回 undefined。

js
const array = [5, 12, 50, 100, 44]
console.log(array.findLast((element) => element > 45)) // 100

console.log(array.find((element) => element > 45)) // 50

Array.prototype.findLastIndex

反向迭代数组,并返回满足所提供的测试函数的第一个元素的索引。若没有找到对应元素,则返回 -1。

js
const array = [5, 12, 50, 100, 44]
console.log(array.findLastIndex((element) => element > 45)) // 3

console.log(array.findIndex((element) => element > 45)) // 2

Array.prototype.toSpliced

js
// 通过 toSpliced() 来删除、添加和替换数组中的元素,并生成一个新的数组
// 比使用 slice() 和 concat() 更高效

const months = ['Jan', 'Mar', 'Apr', 'May']

// Inserting an element at index 1
const months2 = months.toSpliced(1, 0, 'Feb')
console.log(months2) // ["Jan", "Feb", "Mar", "Apr", "May"]

// Deleting two elements starting from index 2
const months3 = months2.toSpliced(2, 2)
console.log(months3) // ["Jan", "Feb", "May"]

// Replacing one element at index 1 with two new elements
const months4 = months3.toSpliced(1, 1, 'Feb', 'Mar')
console.log(months4) // ["Jan", "Feb", "Mar", "May"]

// Original array is not modified
console.log(months) // ["Jan", "Mar", "Apr", "May"]
js
// The toSpliced() method always creates a dense array.

const arr = [1, , 3, 4, , 6]
console.log(arr.toSpliced(1, 2)) // [1, 4, undefined, 6]
js
// The toSpliced() method reads the length property of this.
// It then reads the integer-keyed properties needed and writes them into the new array.

const arrayLike = {
  length: 3,
  unrelated: 'foo',
  0: 5,
  2: 4,
}
console.log(Array.prototype.toSpliced.call(arrayLike, 0, 1, 2, 3))
// [2, 3, undefined, 4]

Hasbang Grammar

在终端可以直接执行 JavaScript 文件,而不需要使用 node 命令。

bash
#!/usr/bin/env node

console.log("Hello, world!");
bash
$ ./hello.js
Hello, world!

Symbols as WeakMap Keys

Symbols 数据类型可用作 WeakMap 中的 key,在之前,只有字符串和对象可以作为 WeakMap 的 key。

code source

js
const map = new WeakMap() // create a weak map

function useSymbol(symbol) {
  doSomethingWith(symbol)

  let called = map.get(symbol) || 0
  called++ // called one more time

  if (called > 2) console.log('Call more than twice')

  map.set(symbol, called)
}

const mySymbol = Symbol('FooBar')
useSymbol(mySymbol)
useSymbol(mySymbol)
useSymbol(mySymbol)

// mySymbol = null;
delete mySymbol // No live references are left to mySymbol, so we can count on the garbage collector eliminating the entry in the weakMap when it runs (eventually)