0

I have an array that was put together by a push method. The elements that were pushed into the array were taken from different divs. now the array is ordered by the way they appear on the dom. I would like to change the order of some of the element so a class attribute could be applied to a new order one at a time through a newly ordered array.

I will show you the JSFIDDLE that has a class added and removed moving along an array. The thing is I have these parent divs layed out by using floats to look like they make a pic frame where the children divs are clickable and content appears inside the frame.( not shown in jsfiddle)

when the added class appears at the end of the right part of the frame it goes to the beginning of the bottom row of divs, which is the bottom part of the frame but i want it to start with the last div of the bottom row ]so it continues travelling through out the frame like a clock. I do not have the frame set up in the jsfiddle I just need to how to cahnge the order so i could apply the classes in that order.

Heres the array now

index :0 =prt1
index :1 =prt2
index :2 =prt3
index :3 =prt4
index :4 =prl5
index :5 =prl6
index :6 =prl7
index :7 =prl8
index :8 =prr9
index :9 =prr10
index :10 =prr11
index :11 =prr12
index :12 =prb13
index :13 =prb14
index :14 =prb15
index :15 =prb16

I want the order of the array to be:

index :0 =prt1
index :1 =prt2
index :2 =prt3
index :3 =prt4 //top right corner
index :4 =prl5
index :5 =prl6
index :6 =prl7
index :7 =prl8 
index :11 =prr12 //botttom right corner the order has to change or the flashing div will    not follow the clock pattern
index :10 =prr11
index :9 =prr10
index :8 =prr9 //bottom left corner
index :15 =prb16
index :14 =prb15
index :13 =prb14
index :12 =prb13 

I need a lesson in changing the order of a group of indexes inside an array so i could apply a function to this new order. thank you.

4

2 回答 2

0

我认为 slice、reverse 和 concat 的组合在这里是最好的:

newArr = oldArr.slice(0,8).concat(oldArr.slice(8,4).reverse())
                                         .concat(oldArr.slice(12,4).reverse());
于 2013-09-16T22:49:45.350 回答
0

听起来你知道你想要的确切顺序,并假设你没有可以创建以后有用的排序算法,你可以像这样硬编码它:

var desiredOrder = [0,1,2,3,4,5,6,7,11,10,9,8,15,14,13,12];
var orderedArray = [];
for(key in desiredOrder){
    orderedArray.push(existingArray[desiredOrder[key]]);
}
existingArray=orderedArray;
于 2013-09-16T22:55:24.387 回答