Access every other item in an array - JavaScript

后端 未结 2 635
有刺的猬
有刺的猬 2021-01-05 08:44

Is it possible for me to access every other item in an array? So basically, all items in positions 0, 2, 4, 6 etc.

Here\'s my code if it helps:

funct         


        
2条回答
  •  余生分开走
    2021-01-05 09:20

    If you just want this with lineLength and not with key, then add a second variable and use += when incrementing:

    function pushToHash(key, value) {
        for (var t = 0, x = 0; t < value.length; t++, x += 2) {
            MQHash[key[t]] = value.slice(0, lineLength[x]);
        }
    }
    

    (The power of the comma operator...)

提交回复
热议问题