JavaScript数组//开始重新排序指定索引处的数组,同时保持原来的顺序 [英] Javascript arrays // reordering an array starting at a specified index while keeping the original order

查看:294
本文介绍了JavaScript数组//开始重新排序指定索引处的数组,同时保持原来的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有以下数组:

  VAR ARR = [A,B,C,D,E]

我竭力要得到一个干净的函数,排序基于指定索引的数组,保持在同一时间的原始顺序

例如,从指数3(所以在这里,从D)排序阵列,将给出如下:

  [D,E,A,B,C]

从索引2:

  [C,D,E,A,B]

等等......

这可能是一些明显的,但我不能让它在我的脑海

帮助AP preciated,THX提前

*编辑*

重复。这里是一个很好的一个。

http://stackoverflow.com/a/7861200/102133


解决方案

  VAR ARR = ['一','B','C','D','E'] ;重新排序功能(数据,索引){
  返回data.slice(指数).concat(data.slice(0,索引))
};的console.log(重排(ARR,3));
的console.log(重排(ARR,2));

Having the following array :

var arr = [a,b,c,d,e];

I'm struggling to get a clean function that sorts the array based on a specified index, keeping at the same time the original order

For example, sorting the array from index 3 (so here, from "d"), would give the following :

[d,e,a,b,c]

From index 2 :

[c,d,e,a,b]

etc...

It might be obvious for some but I can't make it in my mind

Help appreciated, thx in advance

* Edit *

Is a duplicate. Here is a good one.

http://stackoverflow.com/a/7861200/102133

解决方案

var arr = ['a','b','c','d','e'];

function reorder(data, index) {
  return data.slice(index).concat(data.slice(0, index))
};

console.log(reorder(arr, 3));
console.log(reorder(arr, 2));

Example

这篇关于JavaScript数组//开始重新排序指定索引处的数组,同时保持原来的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆