0

When I loop an array or object I frequently use this way:

var arrCars = ["opel" , "audi" , "volvo" , "volkswagen" , 
              "renault" , "porsche" , "infinity" , "lexus"];

for (var i = 0, a; a = arrCars[i]; i++) {
  alert(a);
}

fiddle

It gives clear short code in my opinion instead of e.g. this:

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

In this example it isn't a big issue but when there are some nested objects etc. it gives a lot code.

There are many syntax possibilities like you can see under this question here. But I still can't find out how I can loop trough a part of an array. For example, how can I loop only the first 3 values with my preferred syntax. Ofcourse i can use an extra condition statement, but I'm wondering how I can include this condition in the for syntax itself like: "for (var i = 0; i < 4; i++){}" if it's possible ofcourse.

Community
  • 1
  • 1
Dinizworld
  • 394
  • 1
  • 6
  • 16
  • 1
    I hate your preferred syntax - it's confusing to glance at when skimming ( just my opinion :) ) – tymeJV Jun 20 '14 at 16:20
  • 2
    Your loop doesn't work if the array can contain `0`, `false`, or an empty string. Your second loop doesn't work at all, I think you forgot `i < arrCars.length`. – Barmar Jun 20 '14 at 16:21
  • @tymeJV Maybe I like it because I use aliases often with SQL. For me it's orderly. – Dinizworld Jun 20 '14 at 16:44

4 Answers4

3

I don't like your syntax for the reasons I mentioned in a comment. But to answer the question:

for (var i = 0, a; i < 3 && (a = allCars[i]); i++)

FIDDLE

Barmar
  • 741,623
  • 53
  • 500
  • 612
2

After looking at the suggestions, what you want is this:

for (var i = 0, a; (a = arrCars[i]) && i < 3; i++) {
    ...
}

It seems that you can't put the assignment anywhere other than the first spot (not even parentheses fix that) and You need parentheses around it to prevent assignment to arrCars[i] && i < 3.

I do agree with other comments, you better not have an array item that evaluates to false.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
0

You're on the right track.

var myArray = ["0", "1", "2", "3", "4"];

for (var i = 0; i < 4; i++) {
alert(i);
}

This will throw an alert message for your array elements 0 - 2 (first 3 in array)

mwilson
  • 12,295
  • 7
  • 55
  • 95
  • Sorry, I forgot the condition in my second example, it should be a working example how it can work without an array/object inside. Your solution is not what I was looking for. – Dinizworld Jun 20 '14 at 17:10
0
for (var i = 0, a; ((a = arrCars[i]) || true) && i < 3; i++) {
    doStuff(a);
}

Wrapping the assignment in parenthesis makes sure the assignment doesn't turn into a boolean value, and then OR'ing that assignment's result with true makes sure that an element in the array that evaluates to false doesn't break your for loop. However at this point, that whole for loop line is pretty hideous in my opinion. I've always just done this:

for (var i = 0; i < 3; i++) {
    var a = arrCars[i];

    doStuff(a);
}

It's not quite as concise, but I've always preferred readability over compactness. Let the minifier take care of compacting things.

tophyr
  • 1,658
  • 14
  • 20
  • 1
    I get only **true** when I try your solution. – Dinizworld Jun 20 '14 at 17:01
  • @Dinizworld that's what I get for answering from my phone without trying it! Edited. – tophyr Jun 20 '14 at 18:02
  • @tophyr, I was already thinking in the direction of parentheses. I only tried it around the condition. After your edit it does exactly what I mean. Thanks! – Dinizworld Jun 20 '14 at 18:09
  • 2
    I edited again to fix the "false" element problem, and made some commentary on how ugly that turned the whole line :) I personally prefer manually separating the `a` assignment from the `for` loop logic, though I did find your syntax in the OP intriguing (even if it makes this particular situation a pain). – tophyr Jun 20 '14 at 18:12
  • 1
    Your first loop doesn't work if the array is shorter than 3 elements, it will use `undefined` for the missing elements. The `|| true` makes it ignore whether the element is there. See http://jsfiddle.net/barmar/VJ97Z/5/ – Barmar Jun 20 '14 at 18:18
  • @Barmar no different than `for (var i = 0; i < 3; i++) { doSomething(arrCars[i]); }`. The OP referenced "the first 3 values", which is where my choice of list length came from - I don't think it's unreasonable to expect that the chosen iteration count should be valid. – tophyr Jun 20 '14 at 18:24
  • The whole point of his style of looping is to use the existence of the element as part of the loop test, that's why he has `a = arrCars[i]` in the `for()` block. I interpreted his question as adding an _additional_ condition to limit to the first 3 elements, not remove the test of the element altogether. Otherwise, why put the assignment in the loop test at all? – Barmar Jun 20 '14 at 18:30
  • I see what you mean with the original example but didn't see it that way with the "first 3 values" stipulation added. I saw the assignment mainly as a syntactic trick to perform the aliasing inside the `for` construct. Would be trivial to add a `&& i < arrCars.length` clause as well, but that'd just be ugly++. – tophyr Jun 20 '14 at 18:38