How to change a value in a JavaScript array with multiple values

I have a JavaScript array that can be created using this code

var Test = [];
Test.push([3, 2]);
Test.push([5, 7]);
Test.push([8, 1]);
Test.push([4, 10]);

What I need to do is change the first value in each element in order from 0, the result should look like this:

[0, 2]
[1, 7]
[2, 1]
[3, 10]

I will also accept jQuery's answer.

+3
source share
3 answers
for (var i=0, l=Test.length; i<l; i++){
    Test[i][0] = i;
}
+4
source
for (var i=0; i < Test.length; i++) {
    Test[i][0] = i;
}
+2
source

jquery-ic :

  $(Test).each(function(i) {
        this[0] = i;
    });

, , , . , , .

INCORRECT -

 $(Test).each(function(i) {
        this[0] = i++;
    });
+2

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


All Articles