Strangeness of JavaScript array elements

This is my first question.

I have a JSON string returning AJAX, and after the .done function, passing through:

var arData = [];
$.each(response, function(key, value) {
     var temp = [value[0], value[1]];
    arData.push(temp);
    //console.log(temp);
});
console.log(arData);

When I type var temp, the result is quite normal, something like ["BUFFER STOK", 497206627]. But, when I print out the arData variable, the resulting console log looks like this:

resulting array

How is it that a series of 2-dimensional arrays is displayed as 4 in length, with elements 0, 1 and 3?

Edit: this is the response object log: the resulting response log

***** ***** UPDATE

. ajax json- : [[ "data 1", int value 1], [ "data 2", int value 2],...]. javascript , arData.push([ "data 1", int value 1]);, json-.

:

        var arData = [];
        $.ajax({
          cache: false,
          type: 'get',
          url: 'dtsearch4.php',
          data: {type : 2},
          dataType: 'json',
          async: false
        })
        .done(function(response){ 
          $.each(response, function(key, value) {
             $.each(v,function(key, value){
                  //var temp = [value[0], value[1]];
                  arData.push([value[0], value[1]]);
              });
          });

:

v[Array[2], Array[2],[Array[2],...] v0: Array[4] 0: "APBD-I" 1: 302462864 3: NaN length: 4 __proto__: Array[0] v1: Array[4] v2: Array[4]

= > [4], 0, 1 3? 2?

, 2 :

v[Array[2], Array[2],[Array[2],...] v0: Array[2] 0: "APBD-I" 1: 302462864 length: 2 __proto__: Array[0] v1: Array[2] v2: Array[2]

+4
3

.

$.each(v, function(v_key, v_value) {
     $.each(v_key,function(key, value){
          arData.push([value[0], value[1]]);
      });
});

1st loop will take the values like v0, v1, v2. 
second loop will take the values of v0 like 0,1,3,length,__proto__
+2

,

 <html>
    <head>
    <script>
    function callthis(){
    var arData = [];
    {
        var value=[];
         value[0]="vasim";
         value[1]="vanzara";
         var temp = [value[0], value[1]];
        arData.push(temp);
        //console.log(temp);
    }
    console.log(arData);
    }
    </script>
    </head>
    <body>
        <input type="button" onclick="callthis()" value="Click me">
    </body>
    </html>

JavaScript JQuery , http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml

+1

In this code, jQuery will call a callback function for each element of the object response. templife time exists only until the end of this function. Thus, you only see an array with a length of 2 in the call console.log(temp), because it does not have its previous value.

$.each(response, function(key, value) {
    var temp = [value[0], value[1]];
    arData.push(temp);
    //console.log(temp);
});
-1
source

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


All Articles