-3

I don't know how deep my array is. It could be:

let array = ["1000", "5000", "3000"];

Or:

let array = [ ["4000", "2000", "1000"], ["6000", "7000", "3000"] ];

I would like to convert them in to real numbers. The preferred handler is lodash. But I appreciate every solution.

What I tried was:

let array = array.map(Number);

But that covered one dimensional arrays.

Julian
  • 1,380
  • 12
  • 28
  • Please provide additional information for your down votes, thanks! – Julian Feb 25 '17 at 07:23
  • 2
    Please visit the [help] to see how to ask and you will see we need EFFORT and CODE to help you: _Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [mcve]_ – mplungjan Feb 25 '17 at 07:28
  • 1
    I added my approach. – Julian Feb 25 '17 at 07:31
  • Here is an exact duplicate I found using lodash as one answer. You could have found that too :) http://stackoverflow.com/questions/25333918/js-deep-map-function – mplungjan Feb 25 '17 at 07:33
  • Thanks, @mplungjan. One could doubt the exactness ;) I always do research before I post a question. – Julian Feb 25 '17 at 07:35
  • You would have had less or no downvotes had you told us that and perhaps shared the research :) It is a lodash nested method - sounded very exactly like what you were asking for – mplungjan Feb 25 '17 at 07:45
  • 1
    @mplungian He's not seeking debugging help, so the part you quote does not apply. The problem statement is also perfectly clear. –  Feb 25 '17 at 12:06
  • @torazaburo - the quote fits. It did not even HAVE the code for "why isn't this code working" but now it does. As a member for 5 years you know how this site works and the lack of example code was why it was voted down. Hence my explanation using the nearest "close" explanation available. – mplungjan Feb 25 '17 at 14:06

2 Answers2

1

Use Array#map method with a recursive approach for the nested array.

function conv(arr) {
  // iterate over the array
  return arr.map(function(v) {
    // if the element is an array call the function recursively
    // or parse the number and treat NaN as 0
    return Array.isArray(v) ? conv(v) : Number(v) || 0;
  })
}

const array = [
  ["4000", "2000", "1000", ""],
  ["6000", "7000", "3000"]
];
const array1 = ["1000", "5000", "3000"];

function conv(arr) {
  return arr.map(function(v) {
    return Array.isArray(v) ? conv(v) : Number(v) || 0;
  })
}

console.log(conv(array));
console.log(conv(array1));

With ES6 arrow syntax :

let conv = arr => arr.map(v => Array.isArray(v) ? conv(v) : Number(v) || 0)

const array = [
  ["4000", "2000", "1000", ""],
  ["6000", "7000", "3000"]
];
const array1 = ["1000", "5000", "3000"];

let conv = arr => arr.map(v => Array.isArray(v) ? conv(v) : Number(v) || 0)

console.log(conv(array));
console.log(conv(array1));
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
1

A more general solution would be a version of map which takes any function and works on either arrays OR scalars:

const maybeMap = fn => x => Array.isArray(x) ? x.map(maybeMap(fn)) : fn(x);

const numberify = maybeMap(Number);

const array = ["1000", ["2000", "3000"], "4000"];
console.log(numberify(array));