简体   繁体   English

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

[英]How to get correct array Length in JQuery

I don't know why I getting 2020 array length what process I am missing Please run this code into browser and see result,please give me feedback which I am missing. 我不知道为什么要获得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> 

Arrays make sense for ordered lists of data (which should not have blank spots, such as with sparse arrays). 数组对于有序的数据列表(不应该有空白点,例如稀疏数组)有意义。 Your offline_rijksmuseum_child_barcodes_array is a spare array - you're assigning to an index when [index - 1] does not exist in the array - which results in a very odd structure - 2017 <empty> elements followed by two actual elements. 您的offline_rijksmuseum_child_barcodes_array是一个备用数组-当数组中[index - 1]不存在时,您将分配给索引-这将导致结构非常奇怪-2017年<empty>元素,后跟两个实际元素。 You might consider using an object instead of an array: 您可能会考虑使用对象而不是数组:

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

To get the "length" of the resulting object, you can check the length of its keys : 要获得结果对象的“长度”,可以检查其keys的长度:

Object.keys(offline_rijksmuseum_child_barcodes_array).length

That way, lines like 这样的话

offline_rijksmuseum_child_barcodes_array[key] = obj_new;

will only result in associating the property name [key] with the value obj_new , without also causing spare-array weirdness (like the making the .length of the collection huge in the process). 只会造成财产名关联[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> 

would suggest to construct a new obj like tmpObj and push key & value and then push tmpObj into array. 会建议构造一个新的obj,如tmpObjkeyvalue ,然后将tmpObj推入数组。 and you will get the array lenght also. 并且您还将获得数组长度。

temp object construction 临时对象构造

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

try below code snippet. 请尝试以下代码段。

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM