JavaScript syntax provides the ability to combine variables into arrays and objects. Access to elements can be obtained through indexes and names, through loop operators.
But it is not always possible to know the number of elements in the collection at the development stage and it is not always convenient to use the language syntax within the semantics provided for by it.
Classic designs
The usual loop operator "for (var i = 0; i <aData.length; i ++) {...}" provides access to each element of the array through the index "i" - "aData [i]", and this is convenient when the number elements are known in advance. A similar effect will give the design:
var i = 0;
while (typeof aData [i]! = 'undefined') {... aData [i]; ... i ++; },
when the number of elements is unknown.
You can also build other algorithms for filling and processing arrays, but it is much more convenient to use new, modern options.
A classic is good when you need to process personnel department profiles (surname, name, patronymic, position), equipment cards (model, manufacturer, seller, sales date) and other data that are not structurally changed and are of common interest.
When an array element in itself is an object or structure, has its own properties and methods, then the classics can not cope with the dynamics of quantity and quality, and the array takes on a different meaning.
Cycles for each item
In dynamics, the manifestation of the properties of the current element by an array is essential. The construction of JS foreach array, in a slightly different form than usual (accepted in other languages), allows you to provide the array with the ability to display its properties through its current element.
Suppose one array contains roads (their qualitative and quantitative characteristics), and the other array contains cars that can only drive on certain roads and have fuel tanks of different volumes, that is, the distances between gas stations also matter.
In this version, a suitable algorithm should take, for example, the road and select cars that can drive on it. And it is better if the road and the car, speaking in the program as objects, "find themselves." This is a natural application of object-oriented programming, which, among other things, does not lead to the need to change the algorithm when changing the collections of roads and cars, automatically takes into account roads for repair, cars for maintenance, etc.
Technically, on JS foreach, the construction looks very simple: "aData.forEach (fData);" where fData is a function that applies to each element of the aData array:
function fData (value, index) {sText + = index + '=' + value + '; '; }.
If the source data is presented as follows:
var aData = [1,2,3,4];
aData.push (11);
aData.push (22);
aData.push (44);
aData.push ('line 1');
aData.push ('line 2');
var sText = '',
then the result of this application of JS foreach will be:
"0 = 1; 1 = 2; 2 = 3; 3 = 4; 4 = 11; 5 = 22; 6 = 44; 7 = line 1; 8 = line 2;".
Features of arrays with objects
JavaScript object is special. Using objects in this language differs significantly from implementations in other languages. An object is in itself an array of properties and methods. At the same time, the latter actually perform actions, saving or editing their or external contents of other objects, arrays, variables.
Objects, coming to the array like frames on a film strip, when iterating through the JS foreach object construct, form a new semantics: the construction of mutable meaning .
So, the roads themselves are given the opportunity to choose cars that can ride on them, and the latter filter the routes available for the day. If you do not pay attention to the fact that redundancy in this type of programming increases the reliability of the code, then the task of determining the route of cargo delivery makes the JS foreach design a meaningful and simple solution: lay out routes along the roads, select cars, and perform cargo delivery.
If routes, roads and cars are arrays of objects, then the algorithm is greatly simplified. It would seem strange that the appearance of the JS foreach construct took so long to wait. However, despite the simplicity of what is written, in practice it is quite difficult to implement it.
JS foreach practice and real objects
The thinking of a modern developer operates with objects familiar to programming, but not objects of a real task. Itβs not customary to understand as such a road, a car, a route, a distance ...
Object-oriented programming developed in thorny ways, and it has traditionally become customary to create program objects: an array, a form, a button, a route selection window, etc.
In addition, JS foreach, along with other language constructs, is executed in the visitorβs browser, which introduces serious limitations in practice. Not every developer is ready to share their code. Effective examples of using arrays and collections of real objects are a good way to improve knowledge.
However, the availability of JavaScript code for use not intended by the developer has an indirect effect on the development of promising ideas for creating dynamic and real objects. JS foreach constructions are not yet perfect, and the development of their semantics seems to be a very promising direction.