LEARNING JAVASCRIPT - Trang 169

When you need to…

Use…

In-place or copy

Reverse an array

reverse

In-place

Sort an array

sort

(pass in function for custom sort) In-place

Table 8-3. Array searching

When you want to know/find…

Use…

The index of an item

indexOf

(simple values),

findIndex

(complex values)

The last index of an item

lastIndexOf

(simple values)

The item itself

find

Whether the array has an item that matches some criteria

some

Whether all elements in the array matches some criteria

every

Table 8-4. Array transformation

When you want to…

Use…

In-place or copy

Transform every element in an array

map

Copy

Eliminate elements from an array based on some criteria

filter

Copy

Transform an entire array into another data type

reduce

Copy

Convert elements to strings and join together

join

Copy

Conclusion | 145