所以我有两个非常大的多维数组(4000+)。我从服务器得到第一个数组作为响应,我必须为每个数组元素创建 dom 节点。一旦这个过程完成,我必须发送另一个请求,在那里我将获得另一个元素列表,这将是第一个列表的子集,基于第二个列表我必须修改第一个列表中的一些元素(并将这些更改反射(reflect)在DOM 也是如此)。这个过程需要很长时间才能完成,有什么办法可以不用两个for循环来完成这个过程吗?还是更快的比较?
场景
The real world example would be as follows, consider a group of people in a particular area (arr1). In DOM this would be represented as
CheckBox - Name
Now consider a group of people who have been administered with a particular vaccine (arr2), Now arr2 has the list of elements for which the checkbox should be checked. The whole list(arr1's dom representation) has to be shown at all costs.
数组属于
类型[ ["index", "name", "age"],............. ["4000-index", "4000-name", "4000-age"]]
这是一个伪代码..
//First request, get the response (resp) and Create DOM elements accordingly
for(var i=0, iLen=resp.length; i<iLen; i++)
{
// Checkbox and <span id='entry-"+resp[i][0]+"'>resp[i][1]</span>
}
// At a later stage in the code...
//Request server for the second list get the response (resp)
arr2 = resp // Second Array
// Walk through the dom, get the list of span elements and store their ids in an array arr1
for(var i=0, iLen=arr1.length; i<iLen; i++)
{
for(var j=0, jLen= arr2.length; j<jLen; j++)
{
if(+arr2[j][0] === +arr1[i][0])
{
//checkbox attr = 'checked'
}
}
}
最佳答案
如果您发送作为具有以下结构的对象接收的第二组数据,您可以获得一些非常好的性能提升。
var subset = {
anID:{
bcg: true,
someOtherProp: false,
//and so on
}
}
然后你需要修改你的 DOM 元素 -
for (var id in subset){
//do whatever needs to be done
}
关于javascript - 大型数组和 javascript 比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9990134/