Array built-in methods

how to copy array

simple way

1
2
3
4
5
6
7
const sheeps = ['πŸ‘', 'πŸ‘', 'πŸ‘'];

// Old way
const cloneSheeps = sheeps.slice();

// ES6 way
const cloneSheepsES6 = [...sheeps];

built-in way

The Array.from() method creates a new, shallow-copied Array instance from an array-like or iterable object.

1
2
3
4
5
console.log(Array.from('foo'));
// expected output: Array ["f", "o", "o"]

console.log(Array.from([1, 2, 3], x => x + x));
// expected output: Array [2, 4, 6]

Why Can’t I Use = to Copy an Array?

Because arrays in JS are reference values, so when you try to copy it using the = it will only copy the reference to the original array and not the value of the array.

To create a real copy of an array, you need to copy over the value of the array under a new value variable. That way this new array does not reference to the old array address in memory.

1
2
3
4
5
6
7
8
9
10
const sheeps = ['πŸ‘', 'πŸ‘', 'πŸ‘'];

const fakeSheeps = sheeps;
const cloneSheeps = [...sheeps];

console.log(sheeps === fakeSheeps);
// true --> it's pointing to the same memory space

console.log(sheeps === cloneSheeps);
// false --> it's pointing to a new memory space

how to copy array part

The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.

1
2
3
4
5
6
7
8
9
10
var animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]

console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]

how to modify array

how to sort Array

Array.sort()

1
2
3
4
5
6
7
8
9
var months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]

var array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);
// expected output: ArrayΒ [1, 100000, 21, 30, 4]

Be able to custom sorting for Array

1
2
3
4
5
6
7
8
9
10
function compare(a, b) {
if (a is less than b by some ordering criterion) {
return -1;
}
if (a is greater than b by the ordering criterion) {
return 1;
}
// a must be equal to b
return 0;
}

Example:

1
2
3
4
5
6
7
var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
return a - b;
});
console.log(numbers);

// [1, 2, 3, 4, 5]

Be able to filter Array elements

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

1
2
3
4
5
6
var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

Several method how to iterate Array elements

  • Basic For Loop

  • Lodash forEach

  • The forEach() method executes a provided function once for each array element.

  • The map() method creates a new array with the results of calling a provided function on every element in the calling array.

  • The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.