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

Recommended Answers

All 3 Replies

cdes,

That depends entirely on what you mean by "align".

Airshow[/B

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'];

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.