The array is full of objects in json format, so you can't get the length accurately by using length. How to solve this problem?

var data = ["ab", "c"];

function handleData ( arg ){
    var data = arg;
    console.log(data.length);
}

It"s okay to have strings in the

array, but if you change it to a json object, you can"t get the length correctly

.
var data01 =[{"mid":1,"name":"","content":"","pubtime":"2018-10-21T17:32:57.000Z","examine":0},{"mid":2,"name":"","content":"","pubtime":"2018-10-21T17:33:52.000Z","examine":0},{"mid":3,"name":"","content":"","pubtime":"2018-10-21T17:34:57.000Z","examine":0},{"mid":4,"name":"","content":"","pubtime":"2018-10-21T17:35:50.000Z","examine":0},{"mid":5,"name":"","content":"","pubtime":"2018-10-21T18:00:04.000Z","examine":0},{"mid":6,"name":"","content":"","pubtime":"2018-10-21T18:03:37.000Z","examine":0},{"mid":7,"name":"","content":"","pubtime":"2018-10-21T18:03:50.000Z","examine":0},{"mid":8,"name":"","content":"","pubtime":"2018-10-21T18:05:12.000Z","examine":0},{"mid":9,"name":"","content":"","pubtime":"2018-10-21T18:05:16.000Z","examine":0},{"mid":10,"name":"","content":"","pubtime":"2018-10-21T18:05:50.000Z","examine":0},{"mid":11,"name":"","content":"","pubtime":"2018-10-21T18:06:18.000Z","examine":0},{"mid":12,"name":"","content":"","pubtime":"2018-10-21T18:17:37.000Z","examine":0},{"mid":13,"name":"","content":"","pubtime":"2018-10-21T18:45:13.000Z","examine":0},{"mid":14,"name":"","content":"","pubtime":"2018-10-21T18:45:18.000Z","examine":0},{"mid":15,"name":"","content":"","pubtime":"2018-10-21T18:45:26.000Z","examine":0},{"mid":16,"name":"","content":"","pubtime":"2018-10-21T18:45:41.000Z","examine":0}]


function handleData ( arg ){
    var data = arg;
    console.log(data.length);
}

the output is

data.length = 1493

but the result I want is 16

what caused it

Sep.07,2021

Object.keys (data01) .length


you can also try JSON.parse (data), which is more convenient to use


.

know why

after I use ajax to get the data, the data is of type string, so it makes the length 1493

.
if ( typeof(data) === 'string' ) {
            data = eval('(' + data + ')');
            //console.log(data.length);
            document.getElementsByClassName('floor')[0].value= data.length + "";
        }

convert the string to an array and you can get it correctly

Menu