Question: I'm trying to write a function that will extract and summarize all integers, no matter how many levels deep (I'm talking multidimensional arrays here) from a given array. Here's what I've got so far:

```
function addTheNumbers (someArray) {
    var onlyNumbers = someArray.filter(function(a){ return typeof(a)=='number'; });
    for (i = 0; i &lt; onlyNumbers.length; i++) {
        // idk
    }
}
var sampleArray = ['word',['a','b','c'],12314,'longer phrase',5,[123,123,2],10,3874,32];
addTheNumbers(sampleArray);

```

I have two concerns:

**1)** I feel like the filter I have in place will only extract the numbers that are standing alone within the given array and not ALL numbers in ALL arrays… not sure how to fix it.

**2)** I know that once I've got all the numbers extracted, they'll be stored in an object/array, and I'll have to iterate through it somehow to add them all together, but… once again I have no idea how to proceed.

[JSFiddle](http://jsfiddle.net/w7equtrL/) to make things easier.

Answer: 
Try this.

```js
var temp = [];
function addTheNumbers(someArray) {

 for (var i = 0; i < someArray.length; i++) {
   if(typeof someArray[i] == "number")
   temp.push(someArray[i]);
  else if(typeof someArray[i] == "object"){
   addTheNumbers(someArray[i]);
  }
 };
}

var sampleArray = ['word', ['a', 'b', 'c'], 12314, 'longer phrase', 5, [123, 123, 2], 10, 3874, 32];
addTheNumbers(sampleArray);
```
As the other answers said, the easier way is a recursive structure.

```js
function addTheNumbers(arr) {
    return arr.reduce(function (sum, currentValue) {
        if (currentValue instanceof Array) {
            return sum + addTheNumbers(currentValue);
        }
        return sum + (typeof currentValue == "number" ? currentValue : 0);
    }, 0);
}
var sampleArray = ['word',['a','b','c'],12314,'longer phrase',5,[123,123,2],10,3874,32];
alert(addTheNumbers(sampleArray)); // returns 16483
```