Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript How do I use a JavaScript forEach?

Malcolm

Administrator
Administrator
Staff Team
Code+
Hi all,

I’m trying to use the array method “forEach” but not sure if I’m using it correctly as I keep getting “…not a function”.

So I converted an AJAX response to JSON and want to insert each item into an array. Im doing it this way so I can minimize the stuff that’s not useful to me.

I’m trying to use a forEach on the response JSON to push each object into the array by using this code:

JavaScript:
array.forEach(function(){
   array2.push(this);
});

But this isn’t working, wondering if anyone could explain what I’m doing wrong and how to accomplish what I’m after.

Any help would be greatly appreciated
 
Hey there,
I had a similar issue recently when working with XML files. In my case, I used querySelectorAll, so what was returned was a NodeList and not an array. What worked for me was to convert the list to an array using Array.from(), so something like this for your code:
JavaScript:
Array.from(array).forEach(function(){
   array2.push(this);
});

If this doesn't work, try logging the response to the console to make sure it's actually an array and not an object or other data type.
 
Hey there,
I had a similar issue recently when working with XML files. In my case, I used querySelectorAll, so what was returned was a NodeList and not an array. What worked for me was to convert the list to an array using Array.from(), so something like this for your code:
JavaScript:
Array.from(array).forEach(function(){
   array2.push(this);
});

If this doesn't work, try logging the response to the console to make sure it's actually an array and not an object or other data type.
Hey Johna,

Thanks for sharing! So I found a solution and it's similar to what you suggested. The problem was I was using an array method (forEach) on an object. However, there was an array inside the object - so instead I specified the array inside then used forEach to through each array item.

Example:
JavaScript:
groupObjects.objects.forEach(function(object){
    array.push(object);
});
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom