JS arrays: associative, array to string, array of objects. js: remove array element

Table of contents:

JS arrays: associative, array to string, array of objects. js: remove array element
JS arrays: associative, array to string, array of objects. js: remove array element

Stacking data organization and creating functions with recursive algorithms is a benchmark for determining the quality of a programmer's work. The conventional algorithm has never been the best solution in terms of development, debugging and modification. Only a functionally complete, algorithm that provides recursion on distributed data processing can be considered an evolving solution.

DOM + JavaScript Features

The page loaded into the browser forms a tree of DOM elements that JavaScript can directly access.

Here, JS arrays are the simplest objects, in addition to natural functionality, that implement the stack idea (shift/unshift and push/pop). The selection of the first and last element as a special property of the array plays an important role in the formation of descriptions of data and algorithms.

Recursive function in JavaScript
Recursive function in JavaScript

A recursive JavaScript function can use additional syntax andsemantics not found in other languages. Anonymous functions allow you to equip the syntactic constructions of the language with your own functionality.

Events occur on elements of the DOM tree, and their handlers can change. The work of the JS code is the parallel execution of many processes, the dynamics of which is determined by the general functionality loaded into the browser, the page and the visitor's actions.

Functionality implemented in the form of object methods, combined with the "distribution" of information processing duties between the client and the server, reduces the load on the server and simplifies the JS code.

Variables, arrays and objects

The description of the data determines the algorithm for using it. With this formulation, it is true that the data is primary, and vice versa: the algorithm is primary. “Execution cannot be pardoned” - from the same sphere, the decision is made by the one who puts a punctuation mark or does something else.

Natural language has the highest level of freedom:

  • "expressions of meaning" to those who speak;
  • "understanding the meaning" to those who listen.

The "expression of meaning" by one native speaker is as good as the "freedom of use" by another native speaker.

JavaScript is a programming language. Therefore, there is no freedom and cannot be. Programming is all about rigid syntactic constructions and exact meaning. Even the condition that the type of a variable can be determined only at the time of its use does not give the programmer any freedom.

JavaScript programming language
JavaScript programming language

JavaScript logic isthat it is possible to distinguish a variable from an array only in the context of description or use, and this is an illusory freedom. But the ability to transform anything into anything gives the programmer the necessary tools to build free data structures and data processing algorithms.

JavaScript's logic is similar to that of other languages, but that's where the similarities to other languages end. What can be done in JS can be recreated in other languages, the closer they are to JavaScript in terms of functionality and development.

JavaScript array syntax and semantics

JS arrays are objects. However, another object is a simple variable, but at the right time it can become a system of objects. A simple variable is also an array, if you give it the right functionality.

It is generally accepted that two options for describing an array or object are considered sufficient: "" and "{}". Square brackets are a simple array or object with no methods. Curly braces are an associative array or object with methods. But these are only general provisions. The syntax of the above is much simpler:

js arrays
js arrays

Using this syntax can be quite tricky. The first two examples are ordinary arrays: they are created in a loop, contain letters of the Latin alphabet, differ only in letter case and indices. In the first case, the index is the alphabetical number of the letter, in the second case, the index is the code of the letter.

The third example creates an associative array. Here the letter is the index and its code is the value. Anything is possible if desired.mix up.

The second example immediately demonstrates a feature of JavaScript. The code of the letter "A"=65. On this simple basis, 64 undefined values are automatically placed in the array.

Array functionality

JS arrays don't have many methods, but sort, forEach, filter, map, every/some, reduce/reduceRight functionality deserves special attention. Here you can use your own functions to determine the desired action on the elements of the array.

Strings of characters are a convenient way to represent arrays. An array can always be converted to a string and returned back. JS array into a string is drawn by the method:


All elements of array aData become one string. When merging, you can use a "delimiter", which can be an empty '', a single character ', ', or a string of characters.

When merging array elements by one "separator", nothing interferes in the string method:


Use a different "separator". This mechanism allows you to build quite interesting information processing algorithms, although the main purpose of the transformation "JS arrays - strings" lies in the transmission and storage of data. A string is more compact than an array, more convenient for storing in a database, etc.

Arrays can be supplemented with other arrays (concat method), change the order of elements to reverse (reverse), sort (sort), etc., but the most interesting and practical functionality lies in the first and last elements.

Stackdata organization

Last in, first out - the idea behind the stack. JS arrays excel at providing the functionality of this idea. If we put a new element in an array:


He will be at the end. If you extract the last element of the array (the pop method), then it will be removed from it. The next element retrieval operation will refer back to the last element.

Stacking data organization
Stacking data organization

In fact, adding elements to an array (push) increases it, and removing elements (pop) decreases it. It is important that both methods work at the end of the array.

A similar shift/unshift pair works with the first element of the array, shifting the remaining elements forward or backward. Logically, it is more convenient and clearer to work with the last element of the array, but in some tasks it is important to work with prefixes, not suffixes.

If you combine string merging and stacking, you can further expand the functionality of JavaScript syntax. For example, each element of the fruit array (apple, pear, orange, tangerine, …):

  • Then push/pop on an array element - work with the properties of a particular fruit: color, type, delivery date…
  • Draining all the fruits into one whole - a string and push / pop - these are the properties of the current availability of fruits: total weight, composition, quantity …

Subarrays can be extracted from an array (the slice method) and the original array will remain intact. However, if you delete an array element in js, then it will be changed directly in place. The result of the element removal method(elements) splice no.

fruit array
fruit array

Important. When deleting: the first parameter is the index of the first element to be deleted, the second parameter is the number of elements to be deleted. When extracting a subarray: the first parameter is the index of the first element, the second parameter is the index of the last one. When retrieved, new JS arrays get the number of elements that is between the first and second parameters, inclusive.

Multidimensional JavaScript arrays

JavaScript does not limit the programmer in the number of array dimensions. Nothing prevents you from creating constructions (the indexes are the number of the store, warehouse/shelf, the name of the product/group of products):

  • aShop['NoShop']['BoxF']['Fruits']['Fruit']; // fruits
  • aShop['NoShop']['BoxB']['Berries']['Berry']; // berries
  • aShop['NoShop']['BoxP']['Potatoes']['Potato']; // potatoes

This works. However, if there are several varieties of fruit (berries) in the first two designs, then there is no room for varieties and one more dimension is needed. In the third case, it is possible to indicate varieties of potatoes, but it simply does not have species.

Obviously, in practice, such a solution is cumbersome, time-consuming to modify and test. Not practical. It is best when the dimension is one, maximum two - in very rare cases.

A dynamic and practical JS solution: an array of objects. Here you can immediately select a store as an object, in which there will be shelf objects (warehouses), and on the shelves there will be objects by types, varieties, groups …

With this approach, immediatelydynamics appears for each level of objects. It is possible to add or remove an array in bulk, JS offers concatenation/retrieval and in-place removal methods. In the first case, a group of goods, for example, is moved from a warehouse to a store or vice versa, and in the second case it is simply sold and disappears from the store (removed from the array).

Associations and associative arrays

In recent years, programming has striven for simplicity. The struggle of programming languages for survival showed not who won, but that simplicity is the key to sure success. All languages allow you to create space structures in terms of dimensions, but in practice, modern programmers use two options:

  • simple (index) array;
  • associative array.

Both constructs have indices from zero to the current number of elements. The second also allows you to access the value by some name. According to the general logic of the language, in JS an associative array is an ordered collection of pairs. Moreover, if necessary, the pair "key" - "value" can be interchanged.

If there is an array of 16 pairs: hexadecimal digits, then by creating a simple array of sixteen digits: from 0 to 15, you can select characters (not numbers) as element values: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', ' E', 'F'] and convert the number from decimal to hexadecimal and vice versa.

Associations are an internal property of data. The programmer can use associations to organize data structures at the levelobjects. In this context, the main emphasis is on the creation of real objects, inside which there are associative arrays by data properties.

Arrays of objects

The object in which arrays of properties are located suggests using them according to the meaning of its methods. The essences (types, varieties, properties) of apples and pears are different, they are even more different from citrus fruits or berries. Each fruit or berry (by type and variety, for example) is unique. A general object can describe an abstraction by properties, but the methods here will be common for all types and varieties.

When goods arrive at the store, the array of incoming objects is filled. This array has its own functions for accessing the information contained in it and duplicates the methods of the objects included in it.

Array Application: Fruit Shop
Array Application: Fruit Shop

In this solution, you can, for example, calculate the quantity of goods, both for a specific position, and for all positions at once. You can display any available product, regardless of its type and grade. The method of exactly the product that was accessed will work.

In practice, such an object-oriented approach allows you not to contact the programmer at the stage of operating the object system, since it does not require reprogramming. The programmer provided for the properties, types, varieties and methods of working with the product. That's enough to sell all of the above and more: peaches, cucumbers, eggplants, raisins, nuts…

System of objects in dynamics

Understanding the essence of the information to be processed is extremely important. The more complete the understanding of the subject area and the real objects that it manipulates, the more accurately you can describe the data and develop algorithms for their processing.

Ideal picture of a qualitatively solved problem: a list of elementary objects and systems of objects has been compiled. All properties of each object and all links between them are established.

Ideal Solution
Ideal Solution

Each method of each object is functionally complete, and solves only one task. An object can be accessed only through its methods, and therefore it is not possible to disrupt its functioning from the outside.

The general solution is JavaScript functionality bound via DOM element properties to functions that only access objects via their properties. If it is necessary to develop something, either the interface for communication with the created system of objects is being finalized, or any object as needed.

Popular topic

Editor's choice