Skip to content
Advertisement

Tag: for-loop

How can I Improve my logic by using forEach, not for loop in Javascript

Please check my code first I used axios.all (GET method). Right now, I’m using for loop for combining all the responses to an array. However, What I’m trying to do is using forEach, not for. I have checked https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach , but still working on how. Thank you. Answer Really what you want is to use map (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) because you want

Transform JSON into required format

Here is my input JSON I need to transform json w.r.t parent category and what ever child json elements w.r.t parent to be in one format. All should be dynamic no hard coding comparing with strings. Need to transform my input json to below sampleformat, I tried separating parents and child values in an array and loop it and push.

JavaScript for loop issue affects guess count

so I’m trying to build a JavaScript hangman game and I’m having a problem with my checkMatch function. what I’m trying to achieve is for it to check against the hiddenChoice array and only run the code in the else if statement if this.id isn’t in the array at all. currently if hiddenChoice = apple and this.id = l it

Getting odd results from a for loop inside a filter function

I’m looping inside a filter. I want to get the values from my vals array plus the keys(name, description) for my filter. When I iterate through my vals array, I keep getting returned the name but not the key. Ideally I would like the return method to give me key and value. return x[this.searchValues[i]].includes(‘phil’) to be return x.name.includes(‘phil’) return x.decription.includes(‘phil’)

How can I loop through two sets of numbers?

I’ve got two small puzzles that I can’t quite figure out! I need to loop through two sets of numbers indefinitely, one first and then the other with a 10 second pause between each one in which both values go back to -1. Both start at -1 and both go up to 2. I can loop through the first number

Advertisement