JS - Array methods Flashcards
concat()
The concat() method returns a new array comprised of the array on which it is called joined with the array(s) and/or value(s) provided as arguments.
```js
var arr1 = [1,2,3,4];
var arr2 = [5,6,7,8];
console.log(arr1.concat(arr2)); //[ 1, 2, 3, 4, 5, 6, 7, 8 ]
console.log(arr1); //[ 1, 2, 3, 4 ]
~~~
forEach()
The forEach() method executes a provided function once per array element.
```js
var arr = [1,2,3];
arr.forEach(function (value, index, arr){
console.log(index, value);
});
~~~
some()
The some() method tests whether some element in the array passes the test implemented by the provided function.
```js
var arr = [6,7,85,2,3,5,6,7];
function isBiggerThan5(elem) { return elem > 5; }
console.log(arr.some(isBiggerThan5)); //true
~~~
every()
The every() method tests whether all elements in the array pass the test implemented by the provided function.
```js
var arr1 = [12, 5, 8, 130, 44];
var arr2 = [12, 54, 18, 130, 44];
function isBigEnough(element, index, array) { return element >= 10; }
console.log(arr1.every(isBigEnough)); //false
console.log(arr2.every(isBigEnough)); //true
~~~
map()
The map() method creates a new array with the results of calling a provided function on every element in this array.
```js
var numbers = [1, 4, 9];
var roots = numbers.map(multiply3);
function multiply3(value) { return value * 3; }
console.log(numbers); //[1, 4, 9]
console.log(roots); //[1, 2, 3]
~~~
A more advanced usage ```js var kvArray = [{ key: 1, value: 10 }, { key: 2, value: 20 }, { key: 3, value: 30 }]; var reformattedArray = kvArray.map(function(obj) { var rObj = {}; rObj[obj.key] = obj.value; return rObj; });
console.log(kvArray); //[ { key: 1, value: 10 }, // { key: 2, value: 20 }, // { key: 3, value: 30 } ] console.log(reformattedArray); //[ { '1': 10 }, { '2': 20 }, { '3': 30 } ]
~~~
shift()
The shift() method removes the first element from an array and returns that element. This method changes the length of the array.
```js
var arr = [1,2,3];
arr.shift();
console.log(arr); // [2,3]
~~~
pop()
The pop method removes the last element from an array and returns that value to the caller.
```js
var arr = [1,2,3];
arr.pop();
console.log(arr); // [1,2]
~~~
unshift()
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
```js
var arr = [1,2,3];
arr.unshift(-1, 0);
console.log(arr); // [-1,0,1,2,3]
~~~
push()
The push() method adds one or more elements to the end of an array and returns the new length of the array.
```js
var arr = [1,2,3];
arr.push(4, 5);
console.log(arr); // [1,2,3,4,5]
~~~
.slice()
The slice() method returns a shallow copy of a portion of an array into a new array object.
- *param1:** Zero-based index at which to begin extraction
- *param2:** Zero-based index at which to end extraction.slice extracts up to but not including end
```js
var arr = [‘a’,’b’,’c’,’d’];
console.log(arr.slice(1,1)); //[]
console.log(arr.slice(0,2)); //[‘a’,’b’]
console.log(arr.slice(1,4)); //[‘b’,’c’,’d’]
console.log(arr.slice(5,6)); //[]
~~~
.splice()
The splice() method changes the content of an array by removing existing elements and/or adding new elements.
```js
//Remove the first element
var arr = [1,2,3];
arr.splice(0,1);
console.log(arr); //[2,3]
//Remove the last element
var arr = [1,2,3];
arr.splice(arr.length-1,1);
console.log(arr); //[1,2]
//Remove several elements
var arr = [1,2,3,4,5,6];
arr.splice(2,2);
console.log(arr); //[1,2,5,6]
//Insert an element
var arr = [1,2,3,4,5,6];
arr.splice(2,0,’Banana’);
console.log(arr); //[1,2,’Banana’,3,4,5,6]
//Insert multiple elements var arr = [1,2,3,4,5,6]; arr.splice(2,0,'Banana','Ananas'); console.log(arr); //[1,2,'Banana','Ananas',3,4,5,6]
~~~
filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
```js
function isBigEnough(value) {
return value >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
console.log(filtered); //[12, 130, 44]
~~~
reduce()
The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
```js
var sum = [0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array) {
return previousValue + currentValue;
});
console.log(sum);
~~~
indexOf()
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
```js
var array = [2, 5, 9];
console.log(array.indexOf(5)); //1
console.log(array.indexOf(7)); //-1
~~~