JavaScript Extract Elements From Array

21-Apr-2023

.

Admin

JavaScript Extract Elements From Array

Now, let's see example of javascript extract elements from array. let’s discuss about javascript - how to extract elements of an array. In this article, we will implement a how to extract value from an array in javascript. I explained simply about javascript extract elements from array.

To extract elements from an array in JavaScript, you can use various methods such as:

1. Indexing: You can access individual elements of an array by their index. The first element has an index of 0, the second element has an index of 1, and so on.

Example 1:


<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>JavaScript Extract Elements From Array - NiceSnippets.Com</title>

</head>

<body>

</body>

<script type="text/javascript">

let fruits = ["apple", "banana", "orange"];

console.log(fruits[0]); // Output: "apple"

console.log(fruits[1]); // Output: "banana"

console.log(fruits[2]); // Output: "orange"

</script>

</html>

2. Slice method: The slice() method returns a new array containing a portion of the original array.

Example 2:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>JavaScript Extract Elements From Array - NiceSnippets.Com</title>

</head>

<body>

</body>

<script type="text/javascript">

let colors = ["red", "green", "blue", "yellow"];

let slicedColors = colors.slice(1, 3); // Returns ["green", "blue"]

</script>

</html>

In this example, the slice() method returns a new array containing elements from index 1 to index 2 (excluding the element at index 3).

3. Splice method: The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements.

Example 3:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>JavaScript Extract Elements From Array - NiceSnippets.Com</title>

</head>

<body>

</body>

<script type="text/javascript">

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

numbers.splice(2, 2); // Removes two elements starting from index 2

console.log(numbers); // Output: [1, 2, 5]

</script>

</html>

In this example, the splice() method removes two elements starting from index 2 and modifies the original array accordingly.

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

Example 4:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>JavaScript Extract Elements From Array - NiceSnippets.Com</title>

</head>

<body>

</body>

<script type="text/javascript">

let ages = [20, 25, 30, 35];

let filteredAges = ages.filter(age => age > 25);

console.log(filteredAges); // Output: [30, 35]

</script>

</html>

In this example, the filter() method creates a new array containing only elements that are greater than 25.

#JavaScript