Cookie Consent by Free Privacy Policy Generator ๐Ÿ“Œ 20 JavaScript: Array Methods

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š 20 JavaScript: Array Methods


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

In this tutorial, you will learn JavaScript Array Methods including examples.

JavaScript Array Methods

The following array methods in javascript:

  • Array pop() Method
  • Array push() Method
  • Array toString() Method
  • Array join() Method
  • Array splice() Method
  • Array sort() Method
  • Array shift() Method
  • Array unshift() Method
  • Array reverse() Method
  • Array concat() Method
  • Array slice() Method
  • Array filter() Method
  • Array find() Method
  • Array forEach() Method
  • Array map() Method
  • Array reduce() Method
  • Array some() Method
  • Array every() Method
  • FindIndex() Array Method
  • Array Include() Method
  • IndexOf() Array Method
  • Array lastIndexOf() Method
  • JavaScript Array isArray() Method

1: Array pop() Method
The javascript pop() method is used to remove the last element from an array.

The method returns the value of the removed item.

Ex:-

 var lang = ["PHP", "Python", "Java", "C"];
 lang.pop(); 

Result of the above example is:

Output
// New array:  PHP, Python, Java

2: Array push() Method
The js push() method is used to add a new element to the end from an array.

Ex:-

 var lang = ["PHP", "Python", "Java", "C"];
 lang.push("JavaScript");  

Result of the above example is:

Output
// New array:  PHP, Python, Java, C, JavaScript

3: Array toString() Method
The js toString() method is used to convert array to string.

Ex:-

 var lang = ["PHP", "Python", "Java", "C"];
 lang.toString();

Result of the above example is:

// output
PHP, Python, Java, C

4: Array join() Method
The javascript join() method is used to join the elements of an array into a string.

In otherwords, The "join()" method puts all the elements of the array into a string list. This method difference from "toString()" method.

Ex:-

 var lang = ["PHP", "Python", "Java", "C"];
 lang.join(" - ");

Result of the above example is:

// output
PHP - Python - Java - C

5: Array splice() Method
The javascript splice() method is used to add and remove items from an array.

syntax
================
array.splice(index, howMany, [element1][, โ€ฆ, elementN]);
  • index โˆ’ Index param specifies where a new item should be inserted.
  • howMany โˆ’ An integer indicating the number of old array elements to remove.
  • If howMany set to 0, no items will be removed in array list. Ex:-
 var lang = ["PHP", "Python", "Java", "C"];
 lang.splice(2, 0, "Javascript", "Rust"); 

Result of the above example is:

// Output
new array: PHP, Python, Javascript, Rust, Java, C

6: Array sort() Method
The javascript array sort() method either alphabetic or numeric sorts an array.

Ex:-

 var lang = ["PHP", "Python", "Java", "C"];
 lang.sort();

Result of the above example is:

// Outuput
array: C, Java, PHP, Python

7: Array shift() Method
The javascript shift() method is used to remove the first element from an array.

Ex:-

 var lang = ["PHP", "Python", "Java", "C"]; 
 lang.shift(); 

Result of the above example is:

// Output
new array: Python, Java, C

8: Array unshift() Method
The js unshift() method adds a new element to the beginning of an array.

Ex:-

 var lang = ["PHP", "Python", "Java", "C"]; 
 cars.unshift("JavaScript");   

Result of the above example is:

//Output 
New array: JavaScript, PHP, Python, Java, C

9: Array reverse() Method
The method is used for reverses the order of the elements in an array.

Ex:-

 var lang = ["PHP", "Python", "Java", "C"];             
 lang.reverse();   

Result of the above example is:

// Outuput
array: C, Java, PHP, Python

10: Array concat() Method
The javascript concat() method joins two or more arrays and makes a new one.

Ex:-

 var lang = ["PHP", "Python", "Java", "C"];  
 var newlang = ["JavaScript", "Rust"];  
 var join = lang.concat(newlang); 

Result of the above example is:

// Output
New array: PHP, Python, Java, C, JavaScript, Rust

11: Array slice() Method
The js slice() array() method is used to selected elements in an array and make a new one. It can take one or two arguments.

Ex:-

 var lang = ["PHP", "Python", "Java", "C", "JavaScript"];
 var lang = cars.slice(1, 4); 

Result of the above example is:

//Output 
New Array : Python, JavaScript

12: Array filter() Method
The js filter() method is used to filter the array, according our conditions.

Ex:-

<p>function isCheck(value) {<br>
  return value < 10;<br>
}

var filter = [10, 5, 16, 4, 7, 12].filter(isCheck);

Result of the above example is:

Output
 //new array: 5,4,7

In this example the function filter() creates a new array. Those elements that satisfy the condition checked by isCheck() function.

13: Array find() Method
The js find() method is used find the first element of an array.

Ex:-

function isCheck(value) {
  return value >= 10;
}
var find= [10, 5, 16, 4, 7, 12].find(isCheck);

Result of the above example is:

Output
// 12

14: Array forEach() Method
The JavaScript Array forEach method permits the call of a function on each element of an array in an easy way.

Ex:-

 var num = [18, 12, 10, 15];
 num.forEach(function(item) {
    document.writeln(item);
 });

Result of the above example is:

Output
 // return 
18 12 10 15

15: Array map() Method
The js map() method, creates an array by calling a specific function on each element in the original array.

Ex:-

var numbers = [4, 9, 16, 25];
var x = numbers.map(Math.sqrt);
document.write(x);

Result of the above example is:

 Output
 // return   
 2,3,4,5 

16: Array reduce() Method
The javascript array reduce() method reduces the array to a single value.

Ex:-

var numArr = [
        {  name: 'a', num: 50},
        {  name: 'b', num: 50},
        {  name: 'c', num: 75},
        {  name: 'd', num: 35},
        {  name: 'e', num: 25 },
        {  name: 'f', num: 40 },
    ];

    var sum = numArr.reduce(function (total, currentValue) {
        return total + currentValue.num;
    }, 0);

    document.write( "javascript- Sum of the array value is :- " + sum );

Result of the above example is:

Output
 // return   
 275

17: Array some() Method
The javascript array some() method checks whether at least one element of the array matches the given predicate. if none of the array elements match the predicate, then it will return false

Ex:-

var nums = [3, 18, 19, 20, 25];

function checkNumber(num) {
  return num >= 25;
}

document.write(nums.some(checkNumber));

Result of the above example is:

Output
 // return   
 True

18: Array every() Method
The javascript array every() method checks whether all elements of the array match the predicate:

Ex:-

var nums = [3, 18, 19, 20, 25];

function checkNumber(num) {
  return num >= 3;
}

document.write(nums.every(checkNumber));

Result of the above example is:

 // return   
 true

19: findIndex() Array Method
The javascript findIndex() method is used to find first occurrence position from an array.

let numbers = [1, 2, 3, 4, 5];

let res = numbers.findIndex(e => e % 2 == 1);

document.write(res); //

Result of the above example is:

 // return   
 0

20: Array include() Method
The js includes() method returns true if an javascript array contains a given element; Otherwise, it returns false.

[1,2,3].includes(2); // true
[1,2,3].includes(4); // false
[1,2,3].includes(1,1); // false

Result of the above example is:

 // return   
 true
 false
 false

21: indexOf() Array Method
The js array indexOf() method is used to find the first occurrence position of an array And returns the index of the first occurrence of the element. If the element is not found, -1 return.

var arr = [10, 20, 30, 40, 50, 70, 10, 50];
document.write(arr.indexOf(10)); // 0
document.write(arr.indexOf(10)); // 4
document.write(arr.indexOf(10)); // 5

Result of the above example is:

 // return   
0
4
5

22: Array lastIndexOf() Method
This is similar to the indexOf() method, but one difference in these. The Javascript array lastIndexOf() method is used to find the last occurrence position of an array. It returns -1 if it cannot find the element.

var arr = [10, 20, 30, 40, 50, 70, 10, 40];
document.write(arr.lastIndexOf(10)); // 6
document.write(arr.lastIndexOf(40)); // 7

Result of the above example is:

 // return   
6
7

23: JavaScript Array isArray() Method
The javascript array isArray() method is used to check whether an object is an array. This function returns true if the object is an array, otherwise return false.

var arr = [5, 3, 10, 1, 6, 12]

document.write("Retrun value = " + Array.isArray(arr)); 

Result of the above example is:

Retrun value = true  
...



๐Ÿ“Œ Learn the JavaScript Array.every() and Array.some() methods


๐Ÿ“ˆ 45.24 Punkte

๐Ÿ“Œ JavaScript Array Tutorial โ€“ Array Methods in JS


๐Ÿ“ˆ 45.24 Punkte

๐Ÿ“Œ Convert Array of Starings to Array of Numbers in JavaScript


๐Ÿ“ˆ 33.19 Punkte

๐Ÿ“Œ JavaScript Sort Array - How to Sort an Array Accurately


๐Ÿ“ˆ 33.19 Punkte

๐Ÿ“Œ JS Sum of an Array โ€“ How to Add the Numbers in a JavaScript Array


๐Ÿ“ˆ 33.19 Punkte

๐Ÿ“Œ JavaScript Array Length โ€“ How to Find the Length of an Array in JS


๐Ÿ“ˆ 33.19 Punkte

๐Ÿ“Œ The Javascript higher order array methods with reactjs examples


๐Ÿ“ˆ 32.55 Punkte

๐Ÿ“Œ 20 JavaScript: Array Methods


๐Ÿ“ˆ 32.55 Punkte

๐Ÿ“Œ JavaScript Array Methods: Understand By Implementing


๐Ÿ“ˆ 32.55 Punkte

๐Ÿ“Œ JavaScript Array Methods Cheatsheet


๐Ÿ“ˆ 32.55 Punkte

๐Ÿ“Œ filter vs. find: JavaScript Array Methods


๐Ÿ“ˆ 32.55 Punkte

๐Ÿ“Œ Summary of Common Javascript Array Methods


๐Ÿ“ˆ 32.55 Punkte

๐Ÿ“Œ 17 JavaScript Array Methods Everyone Needs to Know


๐Ÿ“ˆ 32.55 Punkte

๐Ÿ“Œ All the new javascript array methods you missed in 2023


๐Ÿ“ˆ 32.55 Punkte

๐Ÿ“Œ JavaScript Array Methods: Map vs Filter vs Redux


๐Ÿ“ˆ 32.55 Punkte

๐Ÿ“Œ JavaScript Array Methods: A Comprehensive Guide


๐Ÿ“ˆ 32.55 Punkte

๐Ÿ“Œ JavaScript, useState React Array Handling methods.


๐Ÿ“ˆ 32.55 Punkte

๐Ÿ“Œ Unleashing the Power of Customized Array Methods in JavaScript.


๐Ÿ“ˆ 32.55 Punkte

๐Ÿ“Œ [dos] Microsoft Edge Chakra JIT - 'Array.prototype.reverse' Array Type Confusion


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ #0daytoday #Microsoft Edge Chakra JIT - Array.prototype.reverse Array Type Confusion Exploit [#0day #Exploit]


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Microsoft Edge Chakra JIT Array.prototype.reverse Array Type Confusion


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Microsoft Edge Chakra JIT Array.prototype.reverse Array Type Confusion


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Maximize Array sum by adding multiple of another Array element in given ranges


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Print the sum of array after doing k queries on the array


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ How to pass array in another array of objects?


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Convert 1d Array to 2d Array Python


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ How to Create an Array in Java โ€“ Array Declaration Example


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ How to extract value of a property as array from an array of objects ?


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ ES6 Map an Array of Objects to Return an Array of Objects With New Keys


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Generate an Array from given Array according to given conditions


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ FFmpeg Array Access MXF File Out-of-Array Pufferรผberlauf


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ FFmpeg Array Access MXF File Out-of-Array memory corruption


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Bash For Loop Array: Iterate Through Array Values


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Array.from VS Array.prototype.map


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ [dos] Microsoft Windows - Multiple Use-After-Free Issues in jscript Array Methods


๐Ÿ“ˆ 24.73 Punkte











matomo