JS foreach object as a variant construct

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.

Js foreach




, , .

, (, , , ), (, , , ) , .

, , , .





. JS foreach array, ( ) , .

, ( ), - , , .

, , , . , , , " ". - , , , , , ..

Js foreach array




JS foreach : "aData.forEach (fData);", fData - , aData:









function fData(value, index) { sText += index + '=' + value + '; '; }.

:

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 = '',

JS foreach :

"0=1; 1=2; 2=3; 3=4; 4=11; 5=22; 6=44; 7=line 1; 8=line 2;".

JavaScript - . . - . , , , .

, , , JS foreach object : .

, , , . , , - - JS foreach : , .

Js foreach object




, - , . , JS foreach . , , .

JS foreach

, . , , , ...

- , : , , , ..

, JS foreach , . . - .

JavaScript . JS foreach , .




All Articles