在javascript中对数组重新排序
关于这个话题有很多问题/答案。没有一个符合我的具体情况。希望有人能帮忙: 我有一个索引数组,例如:在javascript中对数组重新排序,javascript,arrays,Javascript,Arrays,关于这个话题有很多问题/答案。没有一个符合我的具体情况。希望有人能帮忙: 我有一个索引数组,例如: var indexes = [24, 48, 32, 7, 11]; 以及一组类似于此的对象: var items = [{ name : "whatever", selected : false, loading : true, progress : 55,
var indexes = [24, 48, 32, 7, 11];
以及一组类似于此的对象:
var items = [{
name : "whatever",
selected : false,
loading : true,
progress : 55,
complete : false
},
{
name : "whatever 2",
selected : false,
loading : false,
progress : 100,
complete : true
}];
索引
数组中的每个整数对应于items数组中对象的实际索引
最后,我有一个变量,它定义了items数组中的新插入位置:
var insertindex = ??
我想做的是获取items数组中的所有对象
,这些对象的索引存储在索引
数组中,将它们删除,然后最后将它们放回原来的位置,所有对象都紧挨着放在由变量insertindex
定义的指定索引处
我一直在尝试使用splice()
,方法是将每个索引处的对象复制到一个临时数组中,然后将它们从原始数组中移除,最后循环通过这个新的临时数组,并将它们放回原始项目数组中的新位置,但似乎遇到了心理障碍,无法让它正常工作
总而言之,我只想从items数组中获取与indexes数组中定义的索引匹配的所有对象,将它们放在一起,并在预定义的索引处重新插入它们,然后返回items数组
帮助实现概念可视化。如果您认为该应用程序是一个javascript文件管理器,允许对不必相邻的多个文件选择进行重新排序。索引定义当前选择的数组和定义文件列表的项。最后,rearoderindex
定义所有选定文件应移动到的新插入位置
编辑:正如有人正确地建议的那样,下面是我正在使用的代码:
function reorder(items, indexes, insertindex){
var offset = 0;
var itemscopy = items.slice(0); //make shallow copy of original array
var temparray = new Array(); // create temporary array to hold pulled out objects
//loop through selected indexes and copy each into temp array
for(var i=0, len=indexes.length; i<len; i++){
array[i] = itemscopy[self.cache.selecteditems[i]];
}
//remove all selected items from items array
for(var i=0, len=indexes.length; i<len; i++){
items.splice(indexes[i], 1);
}
//finally loop through new temp array and insert the items back into the items array at the specified index, increasing the index each iteration using the offset variable.
for(var i=0, len=temparray.length; i<len; i++){
items.splice((insertindex+offset), 0, array[i]);
offset++;
}
}
函数重新排序(项、索引、插入索引){
var偏移=0;
var itemscopy=items.slice(0);//对原始数组进行浅拷贝
var temparray=new Array();//创建临时数组以容纳拉出的对象
//循环选定索引并将每个索引复制到临时数组中
对于(var i=0,len=index.length;i您可以使用.splice()
函数向数组中添加元素,以及从数组中删除项。一般原则是:
将索引按升序数字顺序排序
迭代索引
,移除该索引处的元素(调整移除项目的数量),并将其存储在移除项目
数组中
将removedItems
数组重新添加到所需索引处
执行此操作的代码如下所示:
var removedItems = [];
// sort indexes
indexes.sort(function(a, b) {
if(a < b) return -1;
else if(b < a) return 1;
return 0;
});
for(var i = 0; i < indexes.length; i++) {
var index = indexes[i];
removedItems.push(items.splice(index - removedItems.length, 1));
}
var insertIndex = 1;
items.splice.apply(items, [insertIndex, 0].concat(removedItems));
var removedItems=[];
//排序索引
index.sort(函数(a,b){
如果(a
看看。如果您不关心索引的顺序,我建议另一个简短的解决方案:
items.splice.apply(items, [insertIndex, 0].concat(indexes.sort(function(a, b) {
return a - b;
})).map(function(i, p) {
return p > 1 ? items.splice(i - p + 2, 1).pop() : i;
}));
演示:
简而言之,我使用了旧IE浏览器不支持的方法。但是,使用填隙片总是很容易的。发布你的拼接代码,或者更好的是,制作一个JSFIDLE。这听起来像是正确的方法。这绝对是完美的!:)太棒了。系统还提供了一个答案,在最低限度的测试下,这个答案似乎也能起作用。在我给出答案之前,你有没有机会评论一下你认为最好/性能最好的解决方案?非常感谢。这是一个巨大的帮助。@gordyr那么这是否意味着你不关心索引的顺序
数组元素nts?@VisioN在本例中为否。因为项目(文件)随着索引的增加,从左到右、从上到下显示,按升序对它们进行排序是我的用例的正确行为。但是值得注意的是,除了Anthony的答案之外,我还必须测试所选code
索引值中有多少小于“code”插入索引并偏移插入位置这意味着我可以将由数组开头和结尾的项组成的选定文件重新排序到数组中某个插入点middle@gordyr如果是这样的话,我已经添加了我的版本,它稍微短一点,并且不使用额外的资源。这是一个可爱的、非常优雅的解决方案!谢谢Visi接下来,我会彻底测试它,如果它是最好的解决方案,我会在回答你自己的问题时说:)非常感谢!