user10993028
user10993028

Reputation:

Return a 2D array from the parameters of a JavaScript function where a semi-colon means a new row

I have this simple JavaScript functions that works very well that returns an array that contains the parameters (arguments) for example 1,2,3 for that function:

function array() {
var a = Array.from(arguments);
return a; 
} 

I now want to apply the same logic (pass a functions parameters to an array) to a JavaScript function called matrix. Something like this:

matrix(1,2,3;4,5,6) where a semi-colon (;) indicates a new row in the 2D array.

The function take the parameters (or arguments) and returns a 2D JavaScript array like this:

[[1,2,3],[4,5,6]]

What JavaScript code can do this in the most efficient and simplest way?

I have tried to convert the array to a string and then apply split and back to array but I have not managed to get it to work.

The reason why I want to do this is because on my onscreen keyboard on my Microsoft surface tablet it is much easier and much faster to type in a semi-colon than it is to switch keyboard and type in 4 square brackets! Typing in 4 square brackets fast is also often very error-prone. I usually end up with 2-3 brackets when I type fast and then I always have to go back and double check if all 4 brackets were entered correctly and at the right place. A big time killer for me when I type in a lot of JavaScript arrays.

Upvotes: 0

Views: 91

Answers (2)

Seblor
Seblor

Reputation: 7146

I have tried to convert the array to a string and then apply split and back to array but I have not managed to get it to work.

Well if having a string as input works for you, you can simply do it by splitting by ;, then map and split again each sub-array by ,, like this :

const yourString = "1,2,3;4,5,6"

const your2DArray = yourString.split(";").map(subArr => subArr.split(","))

console.log(your2DArray)


Edit :
As sairfan noticed, the code snippet above will not work on IE since it does not support arrow functions. To fix this, you can simply use a plain function :

const yourString = "1,2,3;4,5,6"

const your2DArray = yourString.split(";").map(function (subArr) {
  return subArr.split(",")
})

console.log(your2DArray)

Upvotes: 1

Tyler Roper
Tyler Roper

Reputation: 21672

The main issue with what you're suggesting is that it isn't valid to have a semi-colon within the arguments of a function. This means you'd have to perform the workaround of passing a string instead, which restricts your matrix method to only creating string arrays.

As an alternative, I'd suggest passing an additional argument that you can use to distinguish a break. In the example below, I've used _. This allows you to pass arguments as other types.

const _ = {}; //represents a break

function matrix() {
  let args = Array.from(arguments);
  return args.reduce((out, arg) => {
    let last = out.pop();
    return arg === _ 
      ? [...out, last, []]
      : [...out, [...last, arg]]
  }, [[]]);
}

let result = matrix(1,2,3,_,4,5,6);
console.log(result);

//Pass any type
let variable = true;
let result2 = matrix({},"Hello",_,variable,(5+5));
console.log(result2);

Upvotes: 0

Related Questions