Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[JavaScript] ES2023 Array methods

With ES2023 there are some new Array methods that always return a copy of the original array: toReversed, toSorted, toSpliced, with

const people = [
 { name: "Max", age: 19 },
 { name: "Tom", age: 65 },
 { name: "Liz", age: 43 },
]

// Copying counterpart of the reverse() method
const reversedPeople = people.toReversed()
// [{age: 43, name: "Liz"}, {age: 65, name: "Tom"}, {age: 19, name: "Max"}]

// Copying version of the sort()
const sortedPeople = people.toSorted((a,b) => a.age - b.age)
// [{age: 19, name: "Max"}, {age: 43, name: "Liz"}, {age: 65, name: "Tom"}]

// Copying version of the splice() method
const splicedPeople = people.toSpliced(1,1)
// [{age: 19, name: "Max"}, {age: 43, name: "Liz"}]

// Copying version of using the bracket notation to change the value of a given index
const updatedPeople = people.with(0, { name: "Pat", age: 12})
// [{age: 12, name: "Pat"}, {age: 65, name: "Tom"}, {age: 43, name: "Liz"}]