Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何优化JS中的pull-push元素?_Javascript - Fatal编程技术网

Javascript 如何优化JS中的pull-push元素?

Javascript 如何优化JS中的pull-push元素?,javascript,Javascript,我有1000条记录的数组访问者 在这个数组中,我通过查找元素。idVisitor: let index = this.visitors.findIndex( st => st.idVisitor === _found.idVisitor ); this.visitors.splice(index, 1); this.visitors.unshift(_found); 然后我按索引拉取这个元素并推到数组的顶部 但是这种方法需要很长时间,如

我有1000条记录的数组
访问者

在这个数组中,我通过
查找元素。idVisitor

  let index = this.visitors.findIndex(
          st => st.idVisitor === _found.idVisitor
        );


  this.visitors.splice(index, 1);
  this.visitors.unshift(_found);
然后我按索引拉取这个元素并推到数组的顶部


但是这种方法需要很长时间,如何优化它?

也许这可以帮助您:

以下是我解决问题的方法:

var deleteAtIndex = function(arr, index) 
{
    var len = arr.length;

    if (!len) 
    { 
        return ;
    }

    while (index<len) 
    { 
        arr[index] = arr[index+1]; 
        index++;
    }
    arr.length--;
};
var deleteAtinex=函数(arr,索引)
{
var len=阵列长度;
如果(!len)
{ 
返回;
}
while(indexCheck这个