# forEach & map - Tackling a JavaScript Quirk: A Quick Tip for Similar Hurdles

**URL:** <https://community.integray.com/t/foreach-map-tackling-a-javascript-quirk-a-quick-tip-for-similar-hurdles/224>\
**Category:** Platform discussions\
**Tags:** js-mapper\
**Created:** [November 24, 2023, 9:17am UTC](https://community.integray.com/t/foreach-map-tackling-a-javascript-quirk-a-quick-tip-for-similar-hurdles/224 "2023-11-24T09:17:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veronika](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Veronika](https://community.integray.com/u/Veronika)\
**Post date:** [November 24, 2023, 9:17am UTC](https://community.integray.com/t/foreach-map-tackling-a-javascript-quirk-a-quick-tip-for-similar-hurdles/224/1 "2023-11-24T09:17:05Z")

</div>

Hey everyone,

Recently, I ran into a snag with JavaScript’s `forEach` function that might be useful for others facing a similar issue. When dealing with a lone record, I noticed `forEach` works best with arrays. So, a simple fix—check and transform your input data like this:

```auto
if (!(inData instanceof Array)) {
  inData = [inData];
}

```

This snippet ensures even single entries play nicely with `forEach`. Just a heads-up for anyone navigating the same challenge!

Feel free to share your own tips or experiences. Happy coding! 🌟😊

---

<div class="post-metadata">

**Author:** ![tomas](https://avatars.discourse-cdn.com/v4/letter/t/439d5e/32.png) [@tomas](https://community.integray.com/u/tomas)\
**Post date:** [November 27, 2023, 7:26am UTC](https://community.integray.com/t/foreach-map-tackling-a-javascript-quirk-a-quick-tip-for-similar-hurdles/224/2 "2023-11-27T07:26:09Z")

</div>

Hi Veronika,

yes this is the case of some APIs. In case it returns only one record it is send as an object instead of array. This is the best way to sort out this issue and ensure that you will have only one piece of code to parse your JSON. Then same can be applied for `Array.map`.

Tomas

---

<div class="post-metadata">

**Author:** ![Veronika](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Veronika](https://community.integray.com/u/Veronika)\
**Post date:** [December 4, 2023, 7:26am UTC](https://community.integray.com/t/foreach-map-tackling-a-javascript-quirk-a-quick-tip-for-similar-hurdles/224/3 "2023-12-04T07:26:24Z")

</div>

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