ECMA 14 JavaScript useful array methods
I am going to show you two useful methods of ECMA 14 which we can use in our day to day coding life for arrays
ECMAScript
ECMAScript is the standard that JavaScript is based on. It was developed by the ECMA International standards organization.
ECMAScript 14 is the latest version of the standard, and it was released in June 2023
Array.prototype.findLastIndex()
This methods help us to search a value in an array by iterating through the element in reverse order. It basically returns the index of the first element that meets the condition specified by a call back function, if no element satisfies the condition, then it returns the undefined.
let arr = [12,13,15,99,100,11,44,55]
arr.findLastIndex(num => num <= 50)
Output: 6
So above will iterate the array from the end and return the index of first matching conditions.
Array.prototype.toReversed:
toReversed is the same like reverse() , it just return new array instead of same array memory.
let arr = [12,13,15,99,100,11,44,55]
arr.toReversed()
Output: [55, 44, 11, 100, 99, 15, 13, 12]
arr === arr.reverse() // true
// because both memory pointing are same
arr === arr.toReversed() // false
// because toReversed would return a complete new copy of array with new memory allocation
Same we have toSorted methods which works exact like .sort() methods but it returns the new array with new memory location instead of same memory pointer.
Array.prototype.with
This is new method and allow you to modify a single element based on its index, and get back a new array so if you know the index and the new value then this methods will help you a lot
const names = ["My", "name", "is", "ram"];
names.with(3, "ramkumar")
// ['My', 'name', 'is', 'ramkumar']
It will return the new array with new memory allocation.
Do you want to learn more ?
Follwo me on linkedin