With ES2023 there are some new Array methods that always return a copy of the original array: toReversed, toSorted, toSpliced, with
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 | 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"}] |