Skip to content

Tag: foreach

How to write this function with forEach?

I am trying to group the people by their age and as you can see, my code works with the reduce method. I managed to get this working but now I want to do the same with forEach method. Here’s my code: Answer Reduce accepts initial value that you update each iteration and return. So if you wish to use

Issues with an Array.forEach

Hope someone here would be able to help me out. I am trying to build an Owl-Carousel from an array of objects. but for some reason the array is not been recognized, would appreciate if someone could help to find the mistake I have made here. The error messages I am getting is: ‘item’ is not defined. and ‘tv’ is

Can a some() function replace nested forEach()?

I need to replace two (nested) forEach loops with a some function. The purpose of the code block is to check if the [1] elements in arrayOne are within the max & min of arrayTwo. The code currently works fine and looks like this: Let me know if it isn’t clear enough. Really appreciate your help. Answer Yes, you can

Extract value from the array of objects

I have an array of objects in a variable and I have id of the provider in a different variable. How do I get the name of the provider based on the id. how do I get the name based on the id. when I compare the id variable with with variable containing the array of objects. Array of objects:

addEventListener to all buttons using forEach

I am trying to addEventListener to all of my buttons. This is my first time using addEventListener and it is not running the usergetNumber function which should display a random number when any button is clicked. Answer From top to bottom. There’s already a function usergetNumber() { … } declaration in addEventListener(). It’s a function declaration not a callback here.

How can I assign forEach to variable? Its possible?

Hello everyone:) I’m trying to write rock, paper, scissors game but I have a little problem. Is there any option to assign let playerChoice = buttons.forEach… to any variable as I did in this code? Unfortunately, it doesn’t work like this. I attached my code below. Thanks for any tips! Answer You can’t use forEach to do what you want

Replace selector with array item

I would like to archive that I have an Array, with 3-4 Items and then I want to itterate throw all of them and change the selector in the function with the items of the array. then my function and then the foreach .btn should be like name or person, and I want maybe to add late more Items to

Substring all aria-label elements of an array

HTML Here I have a JS script working to get the number of the active slide in result of a variable. to get it, I’ve substring info from “aria-label” (keeping only slide number before ” / 13″) of the active slide: result // numactiveslide: “4” **How to get the same for not 1 but 3 special slides (prev,active,next). I try