javascript - 大型数组和 javascript 比较

标签 javascript arrays

所以我有两个非常大的多维数组(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/

相关文章:

javascript - 如何使用js从下拉菜单中定位一个选项以应用更多代码

javascript - 对象数组 - 计算属性的平均值?

javascript - 如何采取通过阻塞调用调度的多个操作

c - 循环计数器不递增

javascript - D3.js 汇总嵌套数据

javascript - nodejs 上的 heredoc

javascript - 汉堡包在单击时激活 css 转换

c++ - 数组传递给 C++ 中的方法

javascript - 只除以特定字符串出现的次数,而不是总长度(Javascript)?

java - 从带有两个参数的方法创建新数组java