A function for a for loop

Hey! I’m having some troubles with creating a function that will loop through a desired array when called.

function allLoop(arr) {
  for (var i = 0; i < arr.length; i++) {
    return arr[i];
  }
}

And when called, it returns only one value from an array. Thought when I console log it, it logs all the array values. Is return incapable of returning multiple values? And is there any other way to return those values?
I have a bunch of arrays, and it’s really impractical to write out a for loop for every single array.

Are you sure the arrays have a length greater than one? If it’s returning only one value, the code you supplied should return the value of the index in the array, making me think that the array has only one entry.

V/r,

^ _ ^

Another thing. I’ve never had good experiences when declaring a var within a loop. Try this:

var i;
function allLoop(arr) {
  for (i = 0; i < arr.length; i++) {
    return arr[i];
  }
}

V/r,

^ _ ^

Telling a function to return immediately stops the rest of the function call.
You’re either looking to yield the result if this is a generator, or you’re trying to use an array map.

Okay… I have NOT had enough caffeine, yet. Totally flaked on that. :rofl:

Yeah pretty sure, neither array has one value, some have 3,4, while some have over 20 values, but neither has one :smiley: Will try to remove the var. Thanks for your time :slight_smile:

I’ll look into yield. Thanks!

I’m not sure a generator would help here… you’d only displace the problem, and eventually have to consume the generator in yet another loop:

function * allLoop (arr) {
  for (var i = 0; i < arr.length; i++) {
    yield arr[i]
  }
}

var myArray = [1, 2, 3]

for (var value of allLoop(myArray)) {
  console.log(value)
}

But arrays already implement the iterable protocol anyway, so the generator is not necessary here:

for (var value of myArray) {
  console.log(value)
}

So if you want to do something with each element in the array, use .forEach() (or .map() as @m_hutley suggests, if you need to, well, map each element to another element):

myArray.forEach(function (value) {
  // Do something with value
  console.log(value)
})

// Or in this simple case just
myArray.forEach(console.log)

PS: Going back to your original snippet, if you were to implement that allLoop() yourself, it would look like this:

function allLoop (arr, callback) {
  for (var i = 0; i < arr.length; i++) {
    callback(arr[i])
  }
}

allLoop(myArray, function (value) {
  // Do something with value
})
1 Like

That was really helpful and works great. Thanks a lot! :slightly_smiling_face:

1 Like

If you want to do something with the array putting the return after the for loop could work. eg. (not the most useful example, only to show where the return could go)

function array_to_string(arr) { 
  let arr_str = "";
  for (var i = 0; i < arr.length; i++) { 
    arr_str += arr[i] + " ";
  } 
  return arr_str;
} 

A difference with this vs. a callback is that the loop would need to complete before anything is returned, while a callback could run before the loop completes.

1 Like

Thanks!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.