访问数组中的所有其他项目-JavaScript [英] Access every other item in an array - JavaScript

查看:86
本文介绍了访问数组中的所有其他项目-JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否可以访问数组中的所有其他项?所以基本上所有位置0、2、4、6等的项目.

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:

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

因此,我需要获取所有其他值lineLength.我只想要lineLength,而不是key.我当时在考虑做一个模数,但是不确定如何实现它.有什么想法吗?

So, I need to get every other value of lineLength. I only want this for lineLength, not key. I was thinking of doing a modulus, but wasn't sure how I'd implement it. Any ideas?

提前谢谢!

推荐答案

如果只希望使用lineLength而不使用key,则添加第二个变量,并在递增时使用+=:

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]);
    }
}

(逗号运算符的功能. )

这篇关于访问数组中的所有其他项目-JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆