Skip to content

Latest commit

 

History

History
380 lines (243 loc) · 18.5 KB

28-Array.md

File metadata and controls

380 lines (243 loc) · 18.5 KB

What is an Array ?

An array in JavaScript is a data structure that stores a collection of elements, such as numbers or strings, in a linear fashion. Each element in an array is assigned a unique index starting from 0, which can be used to access and modify its value. Arrays in JavaScript can dynamically grow or shrink in size and support various methods for manipulating their contents.

Need of Array ?

Arrays are an essential data structure in JavaScript as they allow you to store and manipulate collections of data, such as lists of numbers or strings. They provide a way to access elements using index positions, add or remove elements, sort the array, and perform various operations on the data efficiently. Arrays are also useful for organizing and structuring code, and many programming tasks involve working with arrays in some form.

How to create an Array ?

You can create an array in JavaScript by using square brackets []. Here's an example code snippet:

// empty array
let myArray = [];

// array with values
let anotherArray = [1, 2, 3, "hello", true];

In the example above, myArray is an empty array and anotherArray is an array that contains five elements of different data types. Note that arrays in JavaScript are dynamic, which means you can add or remove elements from them at any time.

Array Literal ?

An array literal in JavaScript is a way to create an array by enclosing a comma-separated list of values inside square brackets, like this: [value1, value2, value3]. For example, let myArray = [1, 2, 3]; creates an array named myArray with three elements containing the values 1, 2, and 3. Array literals are a convenient way to initialize arrays with known values.

Array Object ?

In JavaScript, an array is an object that stores a collection of values in a list-like structure. It can hold any type of data, including numbers, strings, objects, and even other arrays. The elements in an array are identified by their index, which starts at 0 for the first element and increments by 1 for each subsequent element. You can create an array using the array literal notation, or by using the Array constructor function. Arrays have many built-in methods for manipulating and iterating over their elements, such as push(), pop(), slice(), forEach(), map(), and reduce().

Index of Array ?

In JavaScript, the index of an array is a numerical value used to access and manipulate its elements. The first element in an array has an index of 0, the second element has an index of 1, and so on. You can access an element in an array using its index by placing the index within square brackets after the name of the array. For example:

let myArray = ["apple", "banana", "orange"];
console.log(myArray[0]); // Output: "apple"
console.log(myArray[1]); // Output: "banana"
console.log(myArray[2]); // Output: "orange"

Array length property ?

In JavaScript, the length property of an array returns the number of elements in the array. For example, if an array has three elements, its length property will return 3. The length property is automatically updated when elements are added to or removed from the array.

Array Declaration ?

In JavaScript, an array can be declared using square brackets with comma-separated values inside the brackets or by calling the Array constructor.

For example:

  1. Using square brackets:
const myArray = [1, 2, 3, 4, 5];
  1. Using the Array constructor:
const myArray = new Array(1, 2, 3, 4, 5);

Note that in both cases, the values inside the array can be of any data type (such as numbers, strings, objects, etc.). Additionally, arrays in JavaScript are dynamic and can grow or shrink as needed.

Looping Array ?

To loop through an array in JavaScript, you can use a for loop or the forEach() method.

Using a for loop:

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

for (let i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}

Using the forEach() method:

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

arr.forEach(function(item) {
  console.log(item);
});

Both methods will output each item in the array to the console.

sort function ?

The sort() function in JavaScript is used to sort an array in ascending or descending order. By default, it sorts the elements in ascending order based on their Unicode values. The basic syntax for using the sort() function is:

array.sort()

To sort an array in descending order, you can use the following syntax:

array.sort((a, b) => b - a)

This uses a compare function that subtracts b from a instead of subtracting a from b, which results in sorting the array in descending order.

Note that the sort() function modifies the original array and returns the sorted array.

push function ?

The "push()" function is a built-in method in JavaScript arrays that adds one or more elements to the end of an array and returns the new length of the array. The syntax for using the push() function is:

array.push(element1, element2, ..., elementN)

where "array" is the name of the array, and "element1", "element2", ..., "elementN" are the elements to be added to the end of the array.

pop function ?

In JavaScript, the pop() function can be used on an array to remove and return the last element of the array. The array's length is reduced by 1 after calling this function. Here's an example:

let fruits = ['apple', 'banana', 'orange'];
let lastFruit = fruits.pop(); // removes 'orange' from the array and returns it
console.log(lastFruit); // output: 'orange'
console.log(fruits); // output: ['apple', 'banana']

unshift function ?

The unshift() function in JavaScript is an array method that adds one or more elements to the beginning of an array and returns the new length of the array. The original array is modified by adding the elements at the beginning. The syntax is as follows:

array.unshift(element1, element2, ..., elementN)

where element1, element2, ..., elementN are the elements to be added to the beginning of the array.

shift function ?

In JavaScript, the shift() function is used to remove the first element from an array and return it. This function also updates the length and indexes of the remaining elements in the array.

Example:

let fruits = ['apple', 'banana', 'orange'];
let shiftedFruit = fruits.shift(); // removes the first element 'apple'
console.log(shiftedFruit); // output: 'apple'
console.log(fruits); // output: ['banana', 'orange']

toString function ?

In JavaScript, the toString function is a method that can be called on an object to convert it into a string representation. By default, the toString method returns a string representing the object. However, some built-in objects like Date, Array, and RegExp override this method to provide a more meaningful string representation of the object. Additionally, developers can also override the toString method for their own custom objects to provide a customized string representation.

join function ?

The join() function is a built-in method in JavaScript that converts an array into a string by concatenating all elements of the array with a specified separator. The syntax for using join() is as follows:

array.join(separator)

Here, separator is an optional parameter that specifies the character to be used between each element of the array. If no separator is provided, a comma will be used as the default.

For example, suppose we have an array of strings:

const myArray = ['apple', 'banana', 'cherry'];

We can use the join() method to convert this array into a single string, separated by commas:

const myString = myArray.join(); // "apple,banana,cherry"

Alternatively, we could specify a different separator, such as a hyphen:

const myString = myArray.join('-'); // "apple-banana-cherry"

concat function ?

In JavaScript, the concat() function is used to merge two or more arrays together and create a new array that contains all the elements from the original arrays. The syntax for concat() is as follows:

newArray = array1.concat(array2, array3, ..., arrayN);

where array1 is the initial array, and array2, array3, ..., arrayN are additional arrays that will be concatenated onto array1. The resulting newArray will contain all the elements from each of the input arrays in the order they were passed into the function.

splice function ?

The splice() function in JavaScript is used to add or remove elements from an array at a specified index. It has the following syntax:

array.splice(startIndex, deleteCount, element1, ..., elementN);
  • startIndex: The index at which to start modifying the array.
  • deleteCount: Optional. The number of elements to remove from the array starting at startIndex. If set to 0, no elements will be removed.
  • element1, ..., elementN: Optional. The elements to be added to the array at startIndex.

The splice() function returns an array containing the deleted elements, or an empty array if no elements were deleted. If elements were added to the array, the original array is modified and returned.

slice function ?

The slice() function in JavaScript creates a new array by copying a portion of an existing array. It takes two arguments: the starting index (inclusive) and the ending index (exclusive) of the portion to be copied. If no ending index is provided, the slice includes all elements from the starting index to the end of the array. The original array is not modified by the slice function.

sort function ?

The sort() function in JavaScript is a built-in method that allows you to sort the elements of an array in place, meaning it modifies the original array. You can call the sort() function on an array and pass in a compare function as its argument to specify how the elements should be sorted. If no compare function is passed in, the elements are sorted alphabetically by default.

Example usage:

const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
numbers.sort(); // sorts alphabetically: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

const ascending = (a, b) => a - b;
numbers.sort(ascending); // sorts numerically in ascending order: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

const descending = (a, b) => b - a;
numbers.sort(descending); // sorts numerically in descending order: [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]

reverse function ?

In JavaScript, you can reverse a string or an array using the reverse() method.

For example, to reverse a string:

let str = "hello";
let reversedStr = str.split("").reverse().join("");
console.log(reversedStr); // Output: "olleh"

And to reverse an array:

let arr = [1, 2, 3, 4];
let reversedArr = arr.reverse();
console.log(reversedArr); // Output: [4, 3, 2, 1]

forEach function ?

The forEach function is a built-in method in JavaScript that is used to iterate over an array or array-like object and execute a callback function for each element. The callback function is passed three arguments: the current element, its index, and the entire array being iterated. The forEach function does not return anything, it simply executes the provided function on each element of the array.

at function ?

The at function in JavaScript is used to retrieve an element from an array at a specific index. It takes one parameter, which is the index of the element to be retrieved. The index can be a positive or negative integer, where negative indices are counted from the end of the array. The function returns the element if it exists at the specified index, and undefined otherwise.

Note that the at function is relatively new and may not be supported by all browsers. In older browsers or environments without support for at, you can use bracket notation (e.g., myArray[index]) instead to access array elements.

map function ?

The map() function in JavaScript is a built-in method available in Array objects. It creates a new array by calling a provided function on each element of the original array. The provided function takes three arguments: the current element being processed, its index, and the array being processed. The map() function does not modify the original array.

filter function ?

The filter() function in JavaScript is an array method that creates a new array with all the elements that pass the test implemented by the provided function. The syntax for using filter() is:

array.filter(callback(element[, index[, array]])[, thisArg])

The callback function takes up to three arguments: the current element being processed in the array, its index, and the array itself. The thisArg parameter is optional and refers to the value of this in the callback function.

For example, to filter out all even numbers from an array, you could use the following code:

const numbers = [1, 2, 3, 4, 5, 6];
const filteredNumbers = numbers.filter(function(number) {
  return number % 2 === 1;
});
console.log(filteredNumbers); // Output: [1, 3, 5]

reduce function ?

The reduce() function in JavaScript is a higher-order function that allows you to iterate over an array and accumulate the values into a single value. It takes a callback function as its first parameter, which is applied to each element in the array, and then returns the final accumulated value. The callback function takes two parameters: an accumulator (which starts with an initial value or the first element of the array) and the current element being iterated over. Here's an example usage:

const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => {
  return accumulator + currentValue;
}, 0); // 0 is the initial value for the accumulator

console.log(sum); // output: 15

In this example, the reduce() function adds up all the numbers in the numbers array and returns the accumulated sum, which is 15.

find function ?

The find() function is a built-in method in JavaScript used to search for an element in an array that satisfies a provided condition. It takes in a callback function and returns the first element in the array that meets the condition specified by the callback, or undefined if no such element is found.

Syntax:

array.find(callback(element[, index[, array]])[, thisArg])

Example:

const numbers = [1, 2, 3, 4, 5];
const foundNumber = numbers.find(number => number > 3);
console.log(foundNumber); // output: 4

findIndex function ?

The findIndex function in JavaScript is an array method that returns the index of the first element in the array that satisfies a provided testing function. If no such element is found, it returns -1. The syntax for using findIndex is as follows:

array.findIndex(function(currentValue, index, arr), thisValue)

The first argument is a callback function that takes in three parameters: currentValue (the current element being processed), index (the index of the current element), and arr (the array being processed). The second argument (thisValue) is optional and sets the value of this when executing the callback function.

Here's an example:

const arr = [2, 4, 6, 8];

const evenIndex = arr.findIndex((element) => element % 2 === 0);

console.log(evenIndex); // Output: 0

In this example, findIndex searches through the arr array and finds the first element that is even, which is 2 at index 0. Therefore, evenIndex is set to 0.

some function ?

Sure, here are a few examples of functions in JavaScript:

  1. console.log(): This function is used to print messages to the browser's console. Example: console.log("Hello World!");

  2. Math.random(): This function is used to generate a random number between 0 (inclusive) and 1 (exclusive). Example: let randomNumber = Math.random();

  3. parseInt(): This function is used to convert a string to an integer. Example: let numString = "42"; let numInt = parseInt(numString);

  4. setTimeout(): This function is used to call a function after a specified number of milliseconds. Example: setTimeout(function() { console.log("Delayed message"); }, 1000);

every function ?

It is not possible to list every function in JavaScript as there are countless built-in functions and libraries, and new ones are constantly being developed by the community. However, there are some commonly used built-in functions that are essential for programming in JavaScript, such as console.log(), setTimeout(), parseInt(), parseFloat(), Math.random(), and many more. Additionally, developers can create their own custom functions to perform specific tasks or manipulate data in unique ways.

flat function ?

In JavaScript, a flat function is used to flatten a multidimensional array into a one-dimensional array. The flat() function returns a new array with all elements flattened recursively up to the specified depth (by default it flattens all levels). Here's an example:

const arr = [1, 2, [3, 4], [5, [6, 7]]];
const flattenedArr = arr.flat(); // [1, 2, 3, 4, 5, 6, 7]

Note that the flat() function is only available in newer versions of JavaScript (ECMAScript 2019), so it may not be supported in older browsers. In that case, you can use a polyfill or a custom implementation to achieve the same result.

flatMap function ?

The flatMap() function in JavaScript is an array method that combines mapping and flattening into a single step. It takes each element in the array, applies a mapping function to it, and then flattens the result into a new array. The resulting array has the same length as the original array, but the elements may be different due to the mapping and flattening process. The flatMap() function can also be used to remove empty or undefined values from an array.

const arr = [1, 2, 3];

const result = arr.flatMap(num => [num, num * 2]);

console.log(result); // [1, 2, 2, 4, 3, 6]

In this example, we start with an array [1, 2, 3]. We then call flatMap() on the array, passing in a function that takes each element of the array and returns a new array containing the original number and that number multiplied by 2.

The flatMap() function then flattens the resulting array, giving us the output [1, 2, 2, 4, 3, 6].

So, the flatMap() function allows us to apply a function to each element of an array, and then flatten the resulting array into a single dimension.