0

I've a for loop which iterates over an array of objects and for each object in the array it returns a method which formats the object.

I was wondering if there's a better substitute for the for loop instead of 'forEach' I've used here. Could you please suggest something?

Here's the code:

for (var index = 0; index < arrItems.length; index++) {
  return formatObj(arrItems[index]);
}

forEach substitue:

var formattedObj;
arrItems.forEach(function (item) {
  formattedObj = formatObj(item);
});
return formattedObj;

Note: I've this loop running inside an else condition.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Sunny
  • 902
  • 3
  • 18
  • 41
  • 1
    what is the actual issue here? your code seems to be correct – Kunal Mukherjee May 15 '19 at 06:44
  • you can use array.map which will return new array of formatted object. – arpit sharma May 15 '19 at 06:46
  • Not sure at all what you want to do, but the two versions don't do the same: Your first version won't go farther than `index=0`, you are `return`ing unconditionnally. Your second one will only grab the value of the last item (but will at least call `formatObj` on every item). – Kaiido May 15 '19 at 06:46
  • Both examples do not even work the same way. – holydragon May 15 '19 at 06:46
  • There's no issue here. Just wondering if there's a better substitute for the 'for' loop. – Sunny May 15 '19 at 06:47
  • in what way `better` may I ask ? – Kunal Mukherjee May 15 '19 at 06:47
  • In terms of fewer lines of code may be! I somehow don't like the 'forEach' loop! – Sunny May 15 '19 at 06:49
  • you might want to look into this : https://stackoverflow.com/questions/5349425/whats-the-fastest-way-to-loop-through-an-array-in-javascript – Shail_bee May 15 '19 at 06:49
  • 1
    Possible duplicate of [What's the fastest way to loop through an array in JavaScript?](https://stackoverflow.com/questions/5349425/whats-the-fastest-way-to-loop-through-an-array-in-javascript) – Jalil May 15 '19 at 06:49

3 Answers3

2

It's probably quite easy to just pass formatObj to forEach:

arrItems.forEach(formatObj);

No return, no index handling, nothing.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

Try this

formattedObj = arrItems.map(fo=>formatObj(fo))
arpit sharma
  • 405
  • 4
  • 11
0

This code:

for (var index = 0; index < arrItems.length; index++) {
  return formatObj(arrItems[index]);
}

Will return the result of formatting the first object in your array.

This code:

var formattedObj;
arrItems.forEach(function (item) {
  formattedObj = formatObj(item);
});
return formattedObj;

Will return the result of formatting the last object in your array.

Are you trying to format your array in-place?

In that case you should know that JS objects are passed by reference, so you can modify them in the callee function, and they will be changed in your array too. So your code can change to:

arrItems.forEach(item => formatObj(item));
return arrItems;

Or, because the forEach method accepts a function with the first argument as the item, you can directly pass that function:

arrItems.forEach(formatObj);
return arrItems;

"Better" is a very subjective term. Better in what sense?

The method used by you is standard JS for iterating over an array of items, and depending on the JS engine running your code, it must be implemented in an optimised way.

varevarao
  • 2,186
  • 13
  • 26