The following are the most useful 8 javascript array method you should know with example
1. Slice
The slice() method selects a part of an array and returns a new array
Example:
const array = ["html","space"];
array.slice(1,1);
//["html","space"]
2. Push
The push () method adds new elements to the end of an array and returns the new length.
Example :
const array = ["html","space"];
array.push("code");
//["html","space","code"]
3. ToString
The toString() method converts an array to a string.
Example:
const array = ["html","space","code"];
const convertedString = array.toString();
console.log(convertedString)
//" html,space, code"
4. Map
The map() method creates a new array with the results of calling a function for every array element.
Example:
const array = [4,6,8,10];
const mappedArray = array.map(item=>item+2);
console.log(mappedArray);
// [6,8,10,12]
5. Shift
The shift() method removes the first element of an array.
Example:
const array = ["html", "space", "code"];
array.shift();
console.log(array)
//["space","code"]
6. Pop
The pop() method removes the last element of an array
Example:
const array = ["html", "space", "code"];
array.pop();
console.log(array)
//["html","sapce"]
7. Filter
The filter() method creates an array filled with all array elements that pass a test.
Example:
const array = ["html", "space", "code"];
const filtered = array.filter(w=>w.length>4);
console.log(filtered)
//["space"]
1 Comments
You forgot the coolest of em all... reduce...
ReplyDeleteIf you have any question please ask?