Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
150
rated 0 times [  152] [ 2]  / answers: 1 / hits: 29208  / 9 Years ago, mon, june 8, 2015, 12:00:00

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


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?



Thanks in advance!


More From » arrays

 Answers
350

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...)


[#66286] Friday, June 5, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
casandra

Total Points: 334
Total Questions: 93
Total Answers: 104

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;