如何在JQuery中获取正确的数组长度

时间:2018-12-04 07:31:35

标签: javascript jquery

我不知道为什么要得到 2020 数组长度,我缺少什么过程。请将此代码运行到浏览器中,查看结果,请给我反馈我所缺少的信息。

var offline_rijksmuseum_child_barcodes_array = new Array();
var offline_rijksmuseum_child_barcodes_new = new Array();
var news = '[{"2018":["testeer","testeer2","testeer3"],"2019":["sd","sd2","sd3"]},{"2018":["dfg"],"2019":["praafd"]}]';
var obj = $.parseJSON(news);
var i = 0;
$.each(obj, function (i, objectData) {
  i++;
  if(i == 1) {
    $.each(objectData, function (key, obj_new) {
      if(key == '2018') {
        offline_rijksmuseum_child_barcodes_array[key] = obj_new;
        //console.log(offline_rijksmuseum_child_barcodes_array);
      }	
      if(key == '2019') {
        offline_rijksmuseum_child_barcodes_array[key] = obj_new;
        //console.log(offline_rijksmuseum_child_barcodes_array);
      }
    });
  } 
  else if(i == 2) {
    $.each(objectData, function (key, obj_new) {
      if(key == '2018') {
        offline_rijksmuseum_child_barcodes_new[key] = obj_new;
        //console.log(offline_rijksmuseum_child_barcodes_new);					
      }	
      if(key == '2019') {
        offline_rijksmuseum_child_barcodes_new[key] = obj_new;
        //console.log(offline_rijksmuseum_child_barcodes_new);
      }
    });
  }
});

console.log(offline_rijksmuseum_child_barcodes_array.length, offline_rijksmuseum_child_barcodes_array);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

2 个答案:

答案 0 :(得分:2)

数组对于有序的数据列表(不应该有空白点,例如稀疏数组)有意义。您的offline_rijksmuseum_child_barcodes_array是一个备用数组-当数组[index - 1]中不存在<empty>时,您将分配给索引-这将导致结构非常奇怪-2017 {{1 }}元素后跟两个 actual 元素。您可能会考虑使用 object 而不是数组:

var offline_rijksmuseum_child_barcodes_array = {};
var offline_rijksmuseum_child_barcodes_new = {};

要获取结果对象的“长度”,可以检查其keys的长度:

Object.keys(offline_rijksmuseum_child_barcodes_array).length

那样,像这样的行

offline_rijksmuseum_child_barcodes_array[key] = obj_new;

将仅 导致将属性名称[key]与值obj_new关联,而不会导致备用数组怪异(例如.length在此过程中非常庞大。

// var x = ['vdf','dsgfdsfds','dsgfdfgdsfds'];
// console.log(x);
var offline_rijksmuseum_child_barcodes_array = {};
var offline_rijksmuseum_child_barcodes_new = {};
var news = '[{"2018":["testeer","testeer2","testeer3"],"2019":["sd","sd2","sd3"]},{"2018":["dfg"],"2019":["praafd"]}]';
var obj = $.parseJSON(news);
var i = 0;
$.each(obj, function(i, objectData) {
  i++;
  if (i == 1) {
    $.each(objectData, function(key, obj_new) {
      if (key == '2018') {
        offline_rijksmuseum_child_barcodes_array[key] = obj_new;
        //console.log(offline_rijksmuseum_child_barcodes_array);
      }
      if (key == '2019') {
        offline_rijksmuseum_child_barcodes_array[key] = obj_new;
        //console.log(offline_rijksmuseum_child_barcodes_array);
      }
    });
  } else if (i == 2) {
    $.each(objectData, function(key, obj_new) {
      if (key == '2018') {
        offline_rijksmuseum_child_barcodes_new[key] = obj_new;
        //console.log(offline_rijksmuseum_child_barcodes_new);                  
      }
      if (key == '2019') {
        offline_rijksmuseum_child_barcodes_new[key] = obj_new;
        //console.log(offline_rijksmuseum_child_barcodes_new);
      }
    });
  }
});

console.log(offline_rijksmuseum_child_barcodes_array);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

答案 1 :(得分:0)

建议构造一个类似tmpObj的新obj,然后将keyvalue推入数组,然后将tmpObj推入数组。并且您还将获得数组长度。

临时对象构建

var tmpObj = {};
tmpObj[key] = tmpObj[key] || [];
tmpObj[key].push(obj_new);

尝试下面的代码段。

var offline_rijksmuseum_child_barcodes_array = new Array();
  var offline_rijksmuseum_child_barcodes_new = new Array();
  
  var news = '[{"2018":["testeer","testeer2","testeer3"],"2019":["sd","sd2","sd3"]},{"2018":["dfg"],"2019":["praafd"]}]';
    var obj = $.parseJSON(news);
    var i = 0;
    $.each(obj, function (i, objectData) {
        i++;                      
        if(i == 1) {
            $.each(objectData, function (key, obj_new) {
              var tmpObj = {};
                if(key == '2018') {
                    tmpObj[key] = tmpObj[key] || [];
                    tmpObj[key].push(obj_new);
                offline_rijksmuseum_child_barcodes_array.push(tmpObj );
                }   
                if(key == '2019') {
                   tmpObj[key] = tmpObj[key] || [];
                    tmpObj[key].push(obj_new);
                    offline_rijksmuseum_child_barcodes_array.push(tmpObj);
                }
            });
        } 
        else if(i == 2) {
            $.each(objectData, function (key, obj_new) {
               var tmpObj = {};                      
                if(key == '2018') {
                  tmpObj[key] = tmpObj[key] || [];
                    tmpObj[key].push(obj_new);
                    offline_rijksmuseum_child_barcodes_new.push(tmpObj);
                }   
                if(key == '2019') {
                tmpObj[key] = tmpObj[key] || [];
                    tmpObj[key].push(obj_new);
                    offline_rijksmuseum_child_barcodes_new.push(tmpObj);
                }
            });
        }
    });

console.log(offline_rijksmuseum_child_barcodes_array.length, '---', offline_rijksmuseum_child_barcodes_array);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>