5

In JavaScript, is it possible to split each string in a multidimensional array of strings using a separator? I'm trying to split a multidimensional array of strings using a string separator, but I don't yet know how to iterate over a multidimensional array without using multiple for-loops.

var theArray = [["Split,each"],["string, in"],["this, array"]];

As far as I know, it isn't possible to apply the string.split(",") method to a multidimensional array. I'll need to find a workaround, since this code isn't valid:

alert([["Split,each"],["string, in"],["this","array"]].split(","));
2
  • 3
    And what should your result be? An array of 3 arrays, or an array of 6 strings? Commented May 12, 2013 at 3:17
  • 1
    @MichaelBerkowski The result should be an array of 3 arrays, where each of the 3 arrays is an array of strings. Commented May 12, 2013 at 3:18

2 Answers 2

8

Use the Array map method to return a modified version of your array:

var newArray = theArray.map(function(v,i,a){
   return v[0].split(",");
});

The function that is passed as the argument to the map method is used to determine the values in the mapped array. As you can see, the function takes each value in the array, splits it by comma, and returns the resulting array of two strings.

The output is then:

[["Split", "each"],["string", "in"],["this", "array"]];

To make this work recursively for arrays of arbitrary depth, you can use:

var newArray = theArray.map(function mapper(v,i,a){
    if(typeof v == "string"){
        return v.split(",");
    } else {
        return v.map(mapper);
    }
});
Sign up to request clarification or add additional context in comments.

5 Comments

No need to include i and a, they're redundant variables. Also, map isn't supported in versions of IE < 9.
@ElliotBonneville Yes, added those from habit.
I keep getting the error Uncaught TypeError: Object [object Array] has no method 'split' jsfiddle.net/4Psmt
@AndersonGreen You copied the code just a second too early. I modified v to v[0], since the elements in your array are arrays.
@AndersonGreen I've added a recursive version if you're interested.
4

You can do this using a traditional for loop:

var theArray = [["Split,each"],["string, in"],["this","array"]];

for(var i = 0; i<theArray.length; i++) {
    theArray[i] = theArray[i].split(",");
}

I'd steer clear of using the map method, it doesn't have great support. (IE < 9 doesn't support it)

3 Comments

This doesn't work for multidimensional arrays of any dimension. It only works for 2-dimensional arrays.
You only provided an example with a 2-dimensional array. Use recursion to iterate through multi-dimensional arrays. I will provide an example of this technique in my answer.
I still don't see a recursive iteration technique here. Is this part still a work in progress?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.