The Unpoly documentation uses the term List
to refer to an array-like value.
There is no actual List
class. Instead that term refers to an abstract interface
for objects that have a { length }
property and allow random access to an indexed element:
list[2] // => 'hello'
list.length // => 10
You can also use them in a for
loop:
for (let element of list) {
console.log(element)
}
Whether a list supports additional utilities like forEach()
or
map()
depends on
the specific type of the list value.
Common types of array-like values include:
Sometimes we want to convert an array-like value into an actual Array
so we can call
methods like map()
or flat()
.
One way to do this is to destructure the array-like value:
let array = [...list]
One drawback of destructuring is that it will always create a new array, even if the given list
is already an Array
.
To only convert non-Array
values, use up.util.toArray()
:
let array = up.util.toArray()