0

这是想法:

var alphabet = ["A","B","C","D","E","F","G","H","I","J","K"]; 
var sequence1 = ["[i]","[i+2]","[i+2]","[i+1]","[i+4]"];

所以基本上我想使用序列'sequence1'遍历'alphabet',这样返回将是:A, C, E, F, J.

我将有许多不同的序列,因此有必要将它们各自存储为自己的数组。

4

2 回答 2

0

您可以将序列存储为整数,然后alphabet像这样迭代:

var alphabet  = ["A", "B", "C", ...];
var sequence1 = [0, 2, 2, 1, 4];

for (var i = 0; i < sequence1.length; i++) {
   alphabet[sequence1[i]+i];
}

请注意,最终输出为A, C, E, F, J. 该顺序应该是0, 1, 2, 2, 3因为数组从索引 0 开始,而不是索引 1。

于 2013-11-09T11:54:40.873 回答
0

您似乎正在使用sequence1描述如何从输出序列中的一个术语跳到下一个术语,因此请尝试以下操作:

var alphabet = ["A","B","C","D","E","F","G","H","I","J","K"],
    sequence1 = [0,2,2,1,4],
    pos = 0,
    newSeq = [];

for (var i = 0, l = sequence1.length; i < l; i++) {
    pos += sequence1[i];

    if (alphabet[pos] !== undefined) {newSeq.push(alphabet[pos]);}
}

newSeq这将产生一个应该是你想要的数组。

演示:http: //jsfiddle.net/BYossarian/uSmg5/

于 2013-11-09T12:13:19.443 回答