Related Topics

Data Structure
import numpy as np
# Creating a 2D NumPy array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Printing the array
print(arr)
Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
// Declare an empty array
let myArray = [];
// Declare an array with elements
let myOtherArray = [1, 2, 3, 4, 5];
// Declare an array with mixed data types
let myMixedArray = ["apple", 3.14, true, {name: "John"}];
In JavaScript, arrays are dynamic in size, which means that you can add or remove elements from an array dynamically.
let myArray = [10, 20, 30, 40, 50];
let thirdElement = myArray[2]; // accessing the third element, which has an index of 2
console.log(thirdElement); // Output: 30
You can also access elements in a nested array by chaining the index notations, like this:
let myNestedArray = [[1, 2], [3, 4], [5, 6]];
let secondElementOfFirstArray = myNestedArray[0][1]; // accessing the second element of the first array
console.log(secondElementOfFirstArray); // Output: 2
In this example, the first index notation myNestedArray[0]
returns the first array [1, 2]
, and the second index notation myNestedArray[0][1]
returns the second element of the first array, which is 2
.
let myArray = [1, 2, 3];
myArray.push(4); // add 4 to the end of the array
console.log(myArray); // [1, 2, 3, 4]
You can also add elements to an array at a specific index using the splice()
method. This method allows you to insert one or more elements into an array at a specified position.
Here’s an example:
let myArray = [1, 2, 3];
myArray.splice(1, 0, 4); // insert 4 at index 1
console.log(myArray); // [1, 4, 2, 3]
The splice()
method takes three arguments: the index at which to start adding or removing elements, the number of elements to remove (in this case, zero), and the elements to add.
array.splice(index, howMany[, element1[, ...[, elementN]]])
index
: The index at which to start changing the array. This parameter is required.howMany
: The number of elements to remove from the array. This parameter is also required.element1, ..., elementN
: The elements to add to the array in place of the deleted elements. This parameter is optional.
For example, suppose you have an array myArray
with the elements ["apple", "banana", "orange", "grape"]
. To remove the element at index 2 (“orange”), you can use the following code:
myArray.splice(2, 1);
After executing this code, the myArray
array will contain the elements ["apple", "banana", "grape"]
.
let arr = [1, 2, 3, 4, 5];
arr.reverse();
console.log(arr); // Output: [5, 4, 3, 2, 1]
You can also create a new array that contains the reversed elements of the original array using a loop:
let arr = [1, 2, 3, 4, 5];
let reversedArr = [];
for (let i = arr.length - 1; i >= 0; i--) {
reversedArr.push(arr[i]);
}
console.log(reversedArr); // Output: [5, 4, 3, 2, 1]
Another way to create a new array with the reversed elements is to use the slice()
method to create a copy of the original array, and then use the reverse()
method on the copy:
let arr = [1, 2, 3, 4, 5];
let reversedArr = arr.slice().reverse();
console.log(reversedArr); // Output: [5, 4, 3, 2, 1]
Note that in the last example, we first use the slice()
method to create a shallow copy of the original array, and then we use the reverse()
method on the copy. This ensures that the original array is not modified.
let arr = [4, 1, 6, 2, 8, 3];
arr.sort(function(a, b) {
return a - b;
});
console.log(arr); // Output: [1, 2, 3, 4, 6, 8]
And here is an example of how to sort an array of strings in alphabetical order:
let arr = ["banana", "apple", "cherry", "date"];
arr.sort();
console.log(arr); // Output: ["apple", "banana", "cherry", "date"]
You can also sort an array in descending order by simply reversing the order of the arguments passed to the comparison function in the sort()
method. Here is an example of how to sort an array of numbers in descending order:
let arr = [4, 1, 6, 2, 8, 3];
arr.sort(function(a, b) {
return b - a;
});
console.log(arr); // Output: [8, 6, 4, 3, 2, 1]
const numbers = [2, 5, 7, 8, 10];
const index = numbers.indexOf(7); // returns 2
The find()
method returns the value of the first element in the array that satisfies the provided testing function. If no values satisfy the testing function, it returns undefined
.
Here is an example:
const numbers = [2, 5, 7, 8, 10];
const result = numbers.find(element => element > 7); // returns 8
In this example, the find()
method is used to find the first element that is greater than 7. The testing function is defined using an arrow function.
# Declaring and initializing an array
my_array = [1, 2, 3, 4, 5]
In this example, my_array
is an array that contains the integers 1, 2, 3, 4, and 5.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.