Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
77 lines (54 sloc) 1.25 KB

Array

Test Array

Array.isArray(array)

This does not work to test if its an array.

if (array) {
  // empty arrays evaluate as false
}

if (array && array.length > 0) {
  // you may still want to capture the array
}

Clone an array

const clone = array.slice(0);

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

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

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

  const array = ['x', 'y', 'z'];
  for (const value of array) {
    console.log(value);
  }
x
y
z

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

Appending/merging arrays together

Reuse Array:

array1.push(...array2);

New Array:

const array3 = array1.concat(array2);
You can’t perform that action at this time.