Skip to content

Tag: arrays

Match two arrays with same id

How to push name from array1 to array2 with matching ids end result Answer Here’s how I’d do it: i.e. create an object out of one of the arrays, using the id as the key. Then you can map over the 2nd one and use the ‘index’ to quickly look up elements in the first one.

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

Swapping array with alternative numbers

In an array take alternate numbers and swap them. Put it again in the place of array again e.g., arr = [8,7,4,6,9,3,2,1] newarr= [8,1,4,3,9,6,2,7] The Code which I tried is below one This is not the correct way to proceed. Answer When you consider every odd indexed element to be alternate –