HappyHands31
HappyHands31

Reputation: 4101

Turn an Array of Numbers Into a String and Back Into an Array of Numbers Within A Single Function With JavaScript

Essentially I'm trying to turn an array of numbers into a string, and then back into an array of numbers (exactly like the original array that was passed in) within a single function.

I know that this will turn an array of numbers into a string:

function arrayToStringAndBack (array) {
    return array.join(" ");
}

console.log(arrayToStringAndBack([1, 2, 3, 4]));

1 2 3 4

But now, if I want to turn that string back into an array, this will return the array with quotes around each number (as an array of string-numbers):

function arrayToStringAndBack (array) {

    let string = array.join(" ");

    return string.split(" ");

}
console.log(arrayToStringAndBack([1, 2, 3, 4]));

[ '1', '2', '3', '4' ]

What I want to do is turn this string 1 2 3 4 into an array of numbers [1, 2, 3, 4].

My idea was to iterate over each element in the string, turning the element into a number using .parseInt(), and then pushing that number into a new array:

function arrayToStringAndBack (array) {

    let string = array.join(" ");

    let newArray = [];

    for (let i = 0; i <= string.length; i++) {
        let number = parseInt(string[i]);
        newArray.push(number);
    }
    return newArray;
}

console.log(arrayToStringAndBack([1, 2, 3, 4]));

But as you can see this logs:

[ 1, NaN, 2, NaN, 3, NaN, 4, NaN]

Why is every other element in numbers NaN? That does not make sense to me.

Upvotes: 0

Views: 600

Answers (4)

In the last part you're trying to convert every position of the string into integers, including spaces. So '1 2 3' turns into [1,NaN,2,NaN,3] because spaces can't be converted to integers.

This modified version of your code should work: function arrayToStringAndBack (array) {

 let string = array.join(" ");
 let numbers = string.split(' ');
 let newArray = [];

    for (let i = 0; i < string.length; i++) {
        let number = parseInt(numbers[i]);
        newArray.push(number);
    }
    return newArray;
}

console.log(arrayToStringAndBack([1, 2, 3, 4]));

Upvotes: 1

James
James

Reputation: 22237

Use map both times!

let array = [1,2,3,222222222];
let arrayOfStrings = array.map(String);
let arrayOfNumbers = arrayOfStrings.map(Number);

console.log(array, arrayOfStrings, arrayOfNumbers);

Upvotes: 1

trincot
trincot

Reputation: 350147

In your second attempt (with parseInt), there are two issues:

  • You do one iteration too many: the <= in the loop condition should be <.
  • You also iterate the spaces that are in the joined string.

You should really combine the idea with the split attempt. So looping over the result from applying split(" ").

But it can be done shorter, by applying Number to each element with map:

return string.split(" ").map(Number)

The Number function does approximately the same as parseInt, except that it does not take more than one argument, which is important when you pass it as argument to .map().

Upvotes: 2

GifCo
GifCo

Reputation: 1363

Use .map()

let array = [ '1','2','3','4']
let arrayOfNumbers = array.map( item => parseInt(item))

Upvotes: 1

Related Questions