0

Sort of at a dead end, I need to find a way to align an element from Array1 with a specified element from Array 2. I.e. A has the index of 1 and I want it to be aligned with e, How would I go about doing this? I have included two example arrays

var Array1 = ['A','B','C','D','E','F','G'];
var Array2 = ['a','b','c','d','e','f'.'g'];

I hope someone can help as been stuck on this for a few days now.

Thanks

cdes1145

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Airshow
0

I have a setup so Array1 can move a certain number of places to the right that I choose, I need a way so that say I choose B in Array1 to align with c in Array2:

var Array1 = ['B','C','D','E','F','G','A'];
var Array2 = ['c','d','e','f','g','a','b'];
0

cdes,

I'm not sure I fully understand but I'll give you my best thoughts.

In general with this sort of thing, you don't need to rearrange the arrays. What you do instead is to pull the data from them with appropriately offset indexes.

You can formulate this many ways. Here's one:

//1. Establish the two arrays
var Array1 = ['A','B','C','D','E','F','G'];
var Array2 = ['a','b','c','d','e','f','g'];

//2. Give the arrays some methods
Array1.setOffset = Array2.setOffset = function(offset){
  this.offset = offset;
}
Array1.getOffsetData = Array2.getOffsetData = function(i){
  return this[(i+(this.offset||0))%this.length];
}

//3. Establish the offsets with the setOffset method.
Array1.setOffset(1);
Array2.setOffset(2);

//Get data from the arrays using the getOffsetData method.
var myIndex = 0;
alert(Array1.getOffsetData(myIndex));
alert(Array2.getOffsetData(myIndex));

//Thus "alignment" of the two arrays is achieved without actually moving the values around

Airshow

Edited by Airshow: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.