# JavaScript One-Dimensional Array Iteration Using Recursion two dimensional array iteration in java,2 dimensional array iteration,

I'm trying to iterate over a simple array using recursion. For this specific case, I'm trying to recreate `.map()` using recursion (without using `.map()`!. I currently am only pushing the last element in the original array, but I want to push all into the array.

``````function recursiveMap (arr, func) {
let newArr = [];
if (arr.length === 1){
newArr.push(func(arr));
}
else {
newArr.push(...recursiveMap(arr.slice(1),func));
}
return newArr;
}``````

You need to to use `func` on the current item, and spread the result of calling the function on the rest of the array:

``````function recursiveMap(arr, func) {
return arr.length ? [func(arr[0]), ...recursiveMap(arr.slice(1), func)] : [];
}

const arr = [1, 2, 3];

const result = recursiveMap(arr, n => n * 2);

console.log(result);``````

Your base case seems wrong. You will need to check for an empty array:

``````function recursiveMap (arr, func) {
let newArr = [];
if (arr.length === 0) {
// do nothing
} else {
newArr.push(func(arr[0]));
newArr.push(...recursiveMap(arr.slice(1),func));
}
return newArr;
}
``````

Instead you will need to call `func` (on the first item) when there is at least one element.

With recursion, I find it is helpful to have the base case be the very first thing you check in your function, and short the execution there. The base case for map is if the array has 0 items, in which case you would return an empty array.

if you haven't seen it before `let [a, ...b]` is array destructuring and a becomes the first value with b holding the remaining array. You could do the same with slice.

``````function recursiveMap(arr, func){
if(arr.length == 0) return [];
let [first, ...rest] = arr;
return [func(first)].concat(recursiveMap(rest, func));
}

let test = [1,2,3,4,5,6,7];
console.log(recursiveMap(test, (item) => item * 2));``````

Below are a few alternatives. Each `recursiveMap`

• does not mutate input
• produces a new array as output
• produces a valid result when an empty input is given, `[]`
• uses a single pure, functional expression

Destructuring assignment

``````const identity = x =>
x

const recursiveMap = (f = identity, [ x, ...xs ]) =>
x === undefined
? []
: [ f (x), ...recursiveMap (f, xs) ]

const square = (x = 0) =>
x * x

console.log (recursiveMap (square, [ 1, 2, 3, 4, 5 ]))
// [ 1, 4, 9, 16, 25 ]``````

## How to transform a matrix into array with one row

Consider a matrix B= [[6,4,1,2], [5,3,9,7],[1,3,2,1]];. B is a matrix with three rows and four columns. I want to treat it as an array or a vector, namely B1=[6,4,1,2,5,3,9,7,1,3,2,1]. Moreover, I ...

Consider a matrix B= [[6,4,1,2], [5,3,9,7],[1,3,2,1]];. B is a matrix with three rows and four columns. I want to treat it as an array or a vector, namely B1=[6,4,1,2,5,3,9,7,1,3,2,1]. Moreover, I ...

1. transform matrix into vector matlab
2. transform matrix into dataframe r
3. transform matrix into diagonal form
4. transform matrix into upper triangular form
5. transform matrix into vector
6. transform matrix into jordan canonical form
7. transform matrix into vector r
8. transform matrix into column excel
9. transform matrix into reduced echelon form
10. transform matrix into row echelon form
11. transform matrix into vector python
12. transform matrix into positive definite
13. transform matrix into dataframe
14. transform matrix into vector excel
15. transform matrix into dataframe python
16. transform matrix into table matlab
17. transform matrix into list python
18. transform matrix into list
19. transform matrix into upper triangular
20. transform matrix in css

## Setting iframe's window.external members through javascript

I have some code running in an html page that is calling a function that exists on window.external. The code works: SomeCode.js (lives in somePage.html): window.external['someFunction'](); However, ...

I have some code running in an html page that is calling a function that exists on window.external. The code works: SomeCode.js (lives in somePage.html): window.external['someFunction'](); However, ...

1. setting iframe height to match content height
2. setting iframe size
3. setting iframe width and height
4. setting iframe src in jquery
5. setting iframe height dynamically
6. setting iframe src in javascript
7. setting iframe height
8. setting iframe source with javascript
9. setting iframe content dynamically
10. setting iframe height to auto
11. setting iframe content
12. setting iframe width
13. setting iframe height dynamically using jquery
14. setting iframe border
15. setting iframe dimensions
16. setting iframe innerhtml
17. setting iframe height in css
18. iframe setting
20. iframe setting in chrome

## How to group by an array and determine total count of rows using javascript

So in sql a common thing to do is a select statement with a group by and then a having count =1 or what have you. select bID from tableA groubBy bID having count(*) = 1 Essentially I am looking to ...

So in sql a common thing to do is a select statement with a group by and then a having count =1 or what have you. select bID from tableA groubBy bID having count(*) = 1 Essentially I am looking to ...

## AngularJS Error: [\$injector:nomod]

Strangely enough the module loads perfectly in Firefox. In Chrome only sometimes the module loads up (when refreshing on and off) and seems to be the only browser where the following console error ...

Strangely enough the module loads perfectly in Firefox. In Chrome only sometimes the module loads up (when refreshing on and off) and seems to be the only browser where the following console error ...

1. angularjs error injector modulerr
2. angularjs error injector unpr
3. angularjs error injector nomod
4. angularjs error injector
5. angularjs injector error unknown provider
6. angularjs uncaught error injector modulerr
7. angularjs ngmaterial injector error
8. angularjs factory injector error
9. angularjs bootstrap injector error
10. error injector modulerr
11. uncaught error \$ injector modulerr http errors angularjs org 1.6 9 /\$ injector modulerr
1