Javascript Array to save an unlimited number of variables

JavaScript is a browser language, therefore not every task needs its own variables, arrays and objects. In many cases, it is quite simple to use the DOM (document object model). But even in such cases, the correct description of the data used always makes the program more advanced, convenient to use and subsequent refinement.

Javascript array

Words have meaning, but, naming in the program that which is subject to use and change, it is appropriate to limit ourselves to the words "data" and "variables". Well, what they will be - JavaScript Array, Object, or just Integer - depends on the task, more precisely on the specific place in it.

Code and data logic

Programmers are used to manipulating data. To this day, there have been variables in a wide range of representations (strings, characters, numbers, dates ...), as a rule, arrays and (the last couple of decades) objects were positioned separately.

Javascript element array

At a certain point in time, strict typing of data became a relic, and now in every modern language, this can change not only the content, but also the type. For better or worse, it will be seen, but strict typing has its positive features. However, if this happened, then why not β€œrip” the whole process?

In the semantic plan, that is, outside the syntax, any variable has a meaning. And it changes in any situation. The sequence of changes is already a quantity, which in terms of JavaScript is an element array.

In other words, if the programmer decided not to attach importance to the type of the variable and entrusted the language with the care of proper and timely type conversion, then why not simplify it at all: there is just data, but what kind of data will they be at the point of application - a number, a string, an array an object - it's up to the code to decide. Even more correctly: this should in itself determine what can be done with it and how exactly.

Simple data and syntax

Simple variables are represented by various types. Type conversions are performed automatically when the need arises.

A simple example of simple syntax

This example describes a simple variable s, in which the result of working with JavaScript Array aCheck will be generated. The variable i is described directly in the cycle of work with aCheck. Here, the elements of the array (created using the JavaScript Array push construct ) are three values ​​of different types. The first loop prints the type names into string s, the second - the actual values. Type conversion is performed automatically.

Javascript array push

About the number of elements in arrays

In JavaScript, Array is not an associative array. Access to the elements is performed using numerical keys, the maximum number of elements is 2 32 , but you should not experiment with the limit quantities of elements.

A good algorithm always has a foreseeable amount of data, and the JavaSscript Array length construct is not introduced into the syntax to control the size of an array in the context of its contents.

The value of aCheck.length will not always be the actual number of elements. In JavaScript, an Array can contain as much of what was sent there, but sometimes you have to work hard to find out exactly how much.

The number of elements - does not always correspond to reality

In this example, the expression aCheck [5] expands the array to six elements. There is no justification for hoping that the length function will give a value of 4.

Associative arrays

Formally, JavaScript Array can only be ordinary, that is, elements are accessed by numeric indexes. However, in reality, you can use associative ideas.

Very simple, classic sorting

A very simple and mundane task of sorting table rows by columns can be solved using an associative array. The dot in the circle next to the column name indicates the absence of sorting, the up arrow - in descending order, the down arrow - in ascending order. Clicking on a column changes direction (JavaScript implementation: Sort Array).

Sort implementation

In this example, sorting directions for each column (S, C, W, ...) are formed in the cSortCols array. The direction values ​​are only u, d. In the for in construct, everything is stitched into one line (both the column code and the sort direction). This loop looks through all the elements of the array, and there is no need to use the JavaScript Array length function.

JavaScript sort array

Creating and sorting an array in JavaScript

Nice practice is to write 'var xMass = [];' or 'var yArr = {};'. In the first case, an ordinary array will be defined, in the second case - associative. You can also use the JavaScript option New Array (), but usually this construction is used for other purposes, mainly for working with objects.

The created array can be immediately filled with variables, but, as a rule, filling and manipulating the array in dynamics during program execution is relevant. If you need sorting, you can use the JavaScript Sort Array construct, which is not flexible, but allows you to control the process through its own function.

A call to: arr.sort () results in sorting in the order of the characters in the ASCII table. The inverse function is the permutation of elements: arr.reverse (). This function reverses the order of array elements.

In both cases, the word arr denotes an array. In the first case, you can use your own sort function, that is, calling arr.sort (myfunc (a, b)) will lead to calling your own function myfuct, which should return a result depending on the data: a and b. If the first is less than the second, then -1, if vice versa, then 1, and if a = b, then 0. Here, the comparison criterion is determined by the programmer in the myfunc function.

Javascript new array

Active element idea

A function called during the sorting process can perform various actions. From the point of view of the construction in which it is applied, it does not have to respond to input parameters, and its result is not only three numbers -1, 1 and 0. Being applied cyclically to each element in the array, it can rebuild the array.

If you imagine that an array is a certain meaning, a certain data structure, then JavaScript Array turns into a variable with variable content.

If we take as a basis the use of the first or last element of the array as its content, then the execution of the sort function can transform the array so that the first / last becomes a different element, as required by the conditions of the task.

Such an idea may allow us to rebuild the solution algorithm in such a way, in particular, that there will be no need to use the if () {} else {} and switch () {case '' constructs: ...; case '': ...; ...}.

By manipulating the contents of the array, you can move the elements inside it, thereby changing the external functionality that can be obtained through the first or last element.

JavaScript array length

JavaScript Array: illogical use

Every language is good in that it allows not only to change the structure and content of the data, but also the actual code. The idea of ​​considering a variable as an abstraction, that is, without initially providing for its typical status, opens up new horizons.

Starting with abstraction, a variable, an array, or an object allows us to represent the data processing as a function of this data, and in dynamics.

For example, reading a text, you can describe this "text", which will be divided into sentences - the criterion of "point" (in the context, that is, taking into account the accepted syntax of the sentences). Sentences will be broken down into phrases (in context, comma and corresponding syntax). Further words and letters.

Result: you can work at the level of texts, sentences, words. At each level, you can create "uniqueness" functions, which allows you to search for similar or similar. You can create functions for applying sentences, phrases to other data.

Modern programming is not a dogma

Modern programming has not been a dogma for a long time, but the fact that the syntactic constructions created over the past decades and accumulated experience open new horizons unintended by language developers has yet to be opened and used.

Source: https://habr.com/ru/post/K4299/


All Articles