Tip 5: Create Flexible Collections with Arrays
Explore how JavaScript arrays provide flexible ways to manage data collections through ordering, adding, removing, and transforming items. Understand array methods like map, filter, and reduce, and discover how arrays serve as bridges to other collection types.
We'll cover the following...
In JavaScript, there used to be only two structures for collections of data: arrays and objects. That list is growing. Now there are maps, sets, weakmaps, weaksets, objects, and arrays.
Working with arrays
When choosing a collection, you have to ask yourself what you need to do with the information. If you need to manipulate it in any way (add, remove, sort, filter, alter all members), then arrays are often the best collection. And even when you don’t use an array, you’ll almost certainly use ideas that you’d associate with arrays.
Arrays have a remarkable amount of flexibility. Because arrays preserve order, you can add and remove items according to their position or determine if they have a position at all. You can sort to give the array a new order as you’ll see in Tip 9, Avoid Sort Confusion with the Spread Operator.