简体   繁体   English

将“未定义”索引映射到数组

[英]Map “undefined” index to Array

Let's say we have an Array like so: 假设我们有一个像这样的数组:

var example = ["a", "b", "c", "d", "e"]

We know that we can access an index of 0-4: 我们知道我们可以访问0-4的索引:

example[0] == "a"
example[1] == "b"
example[2] == "c"
example[3] == "d"
example[4] == "e"

However, we also want to map an undefined index. 但是,我们也想映射一个undefined索引。 For example: 例如:

example[0] == example[5] == example[10] == example[15] == example[20] == "a"
example[1] == example[6] == example[11] == example[16] == example[21] == "b"
example[2] == example[7] == example[12] == example[17] == example[22] == "c"
example[3] == example[8] == example[13] == example[18] == example[23] == "d"
example[4] == example[9] == example[14] == example[19] == example[24] == "e"

Though it may be simple, the math is simply escaping my mind. 尽管这可能很简单,但数学只是在逃避我的注意。 Keep in mind the example.length is not fixed and may be different at times. 请记住example.length不是固定的,有时可能会有所不同。 How would one go about this? 怎么会这样呢?

您似乎想使用模数

example[i%example.length]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM