Arrays in the programming world

Arrays, we’re all too familiar with, are included in any language, just in slightly different forms. In all programming languages, there is an array data type. Arrays, however, are not just data types; they are the most common data structures in the world of computer programming. Arrays are built-in types in programming languages and are often very efficient. Data stores that meet different requirements.

2. Array definition

The standard definition of an array is that it is a linear collection of stored elements that can be accessed arbitrarily by an index, usually a number, that calculates the offset between the stored locations of the elements. A linear table data structure that uses a contiguous set of memory Spaces to store a set of data of the same type. Arrays have two key words: linear set and continuous memory space.

Linear set:

As the name suggests, it is a collection arranged in a line, and the data on each collection can only be divided into the front and the back. In addition to arrays, stacks, queues, linked lists, and so on are linear table structures. The opposite of a linear structure is a nonlinear structure, which is a collection of data that is not arranged in a straight line. Binary trees, heaps, graphs and so on are nonlinear table structures.

Continuous memory space:

The allocation of memory addresses is continuous. The first memory address can be used to calculate the memory address of the other elements in the collection. Because of this feature, arrays are “randomly accessible”. The addressing formula for the elements in the array is: A [k]_address = base_address + K * type_size.

Three. Array methods

1. Access function:

JavaScript provides a set of functions that access array elements, called access functions, and return some variation of the target array.

1.1. IndexOf ()

The indexOf() function is one of the most commonly used accessors to find if the passed argument exists in the target array. If the target array contains this parameter, the index of the element in the array is returned; If it does not, -1 is returned.

1.2. String representation of arrays

There are two methods to convert arrays to strings: Join () and toString(). Both methods return a string containing all the elements of the array, separated by commas.

1.3. Create a new array from an existing array

The concat() and splice() methods allow you to create new arrays from existing arrays. The concat method merges multiple arrays to create a new array, and the splice() method takes a subset of an array to create a new array.

2. Variable functions:

JavaScript has a set of mutable functions that you can use to change the contents of an array without referring to an element in it. These functions often simplify and make difficult things easy.

2.1. Add elements to an array

There are two methods to add elements to an array :push() and unshift(). The push() method adds an element to the end of the array, and the unshift() method adds an element to the beginning of the array.

2.2. Remove elements from an array

The pop() method removes the last element of the array, and the shift() method removes the first element of the array.

2.3. Add and remove elements from the middle of an array

Deleting the first element in an array has the same problem as adding an element to the beginning of the array — both operations need to move the remaining elements in the array forward or back, but the splice() method helps us do either. To add an element to an array, use the splice() method with the following parameters:





• Initial index (where you want to start adding elements);


• Number of elements to delete (set this parameter to 0 when adding elements);


• The element you want to add to the array.

2.4. Sort arrays

The first method is reverse(), which reverses the order of the elements in the array. If the elements are strings, the sort() method sorts the elements in the array lexicographically

Iterator method:

These methods apply a function to each element in the array and can return a value, a set of values, or a new array.

3.1. Iterator methods that do not generate new arrays

ForEach () — this method takes a function as an argument and uses that function forEach element in the array.


Every () – This method takes a function that returns a Boolean type and uses that function for each element in the array. This method returns true if the function returns true for all elements.


Some () — This method also accepts a function that returns a Boolean value, and returns true as long as there is an element that causes the function to return true.


Reduce () – This method takes a function and returns a value. The method starts with a cumulative value and calls the function on the cumulative value and subsequent elements in the array until the last element in the array returns the resulting cumulative value.


JavaScript also provides the reduceRight() method, which, unlike reduce(), executes from right to left.

3.2. Iterator methods that generate new arrays

There are two iterator methods that generate new arrays :map() and filter(). Map () is a bit like forEach() in that it uses a function forEach element in the array. The difference is that map() returns a new array whose elements are the result of applying a function to the original elements.

conclusion

An array in JavaScript is a special type of object, and the index used to represent an offset is an attribute of that object, which may be an integer. However, these numeric indexes are internally converted to string type because the property names in JavaScript objects must be strings. Arrays are just special objects in JavaScript, so they are not as efficient as arrays in other languages. Arrays in JavaScript, technically called objects, are special JavaScript objects that are internally classified as arrays. Because Array is treated as an object in JavaScript, it has a number of properties and methods that can be used programmatically.

Arrays are the simplest, most basic data structures.

Arrays use a contiguous memory space to store a group of data of the same type. The biggest feature of arrays is that they support random access. But insert and delete are inefficient, with an average time of O(n).