[英]jquery/javascript -how to loop through array and create variables in each iteration?
[英]How do I create a sparse array in javascript through iteration?
我編寫此代碼是從一系列dom元素中刮取一個稀疏數組。 一維完成時,代碼有效,但二維中失敗。 有什么我想念的嗎?
23 function initCellHover(){
24 $cells.each(function(){
25 var arrayX = $(this).position().left/cellWidth;
26 var arrayY = $(this).position().top/cellHeight;
27 var arrayValue = $(this);
28 cellLookup[arrayX][arrayY] = arrayValue;
29 });
30 }
在第28行中,您可能引用的是undefined屬性。 檢查數組中是否已存在屬性,並在需要時添加它是有意義的:
cellLookup[arrayX] = cellLookup[arrayX] || [];
cellLookup[arrayX][arrayY] = arrayValue;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.