Array

Test Array

1
Array.isArray(array)
Copied!
This does not work to test if its an array.
1
if (array) {
2
// empty arrays evaluate as false
3
}
4
5
if (array && array.length > 0) {
6
// you may still want to capture the array
7
}
Copied!

Clone an array

1
const clone = array.slice(0);
Copied!

Iterate keys in an array (for...in)

1
const array = ['x', 'y', 'z'];
2
for (const key in array) {
3
console.log(key);
4
}
Copied!
1
0
2
1
3
2
Copied!

Iterate values in an array (for...of)

1
const array = ['x', 'y', 'z'];
2
for (const value of array) {
3
console.log(value);
4
}
Copied!
1
x
2
y
3
z
Copied!

Appending/merging arrays together

Reuse Array:
1
array1.push(...array2);
Copied!
New Array:
1
const array3 = array1.concat(array2);
Copied!
Last modified 2yr ago