1

I want a column vector as my output but instead getting a single dimension array. Please check the code in the fiddle. What have I done wrong?

My Current and Expected output are:

Current Output: Array [ 0, 1 ] Array [ 5, 10 ]

Expected Output: Array [ 0 ] Array [ 1 ] Array [ 5 ] Array [ 10 ]

Please check Browser Console.

How can I get my Expected Output?

Js Fiddle: https://jsfiddle.net/4o1dj1uw/2/

Code:

this.b = [[[0,1]],[[5,10]]];
function convertarray(arrToConvert) {
   //it will convert a matrix in array
   var newArr = [];
   for (var i = 0; i < arrToConvert.length; i++) {
        newArr = newArr.concat(arrToConvert[i]);
   }
   return newArr;
 }

console.log(...convertarray(this.b))
3
  • Just put it twice through convertarray(): console.log(convertarray(convertarray(this.b))) I'm not sure if that's what you want, but the result is one array Commented Mar 16, 2018 at 12:35
  • Is this a one time thing or do you see yourself doing this multiple times throughout the code? Commented Mar 16, 2018 at 13:40
  • Thank you! the array in this.b is actually inputted by the user. So I need individual array of all elements given as input. This needs to happen only once. Commented Mar 21, 2018 at 6:22

2 Answers 2

2

Almost there, you need to enter to the deepest array.

An alternative is using recursion.

this.b = [[[0,1]],[[5,10]]];
function convertarray(arrToConvert) {
   //it will convert a matrix in array
   var newArr = [];
   for (var i = 0; i < arrToConvert.length; i++) {
        if (Array.isArray(arrToConvert[i])) {
          newArr = newArr.concat(convertarray(arrToConvert[i]));
        } else {
          newArr.push([arrToConvert[i]]);
        }
   }
   return newArr;
 }

console.log(JSON.stringify(convertarray(this.b)))
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Ele Recursion is a good idea but this would not give me an array for every element in this.b right?
1

Push it twice through convertarray():

console.log(convertarray(convertarray(this.b)))

If you want to get an array of each item of these you can do:

console.log(...convertarray(convertarray(this.b)).map(x => [x]));

Result

[0] [1] [5] [10]

2 Comments

Thanks a lot @Highmastdon this method worked for me. Actually in my code [0,1] and [5,10 ]is given by user so suppose if he only enters an integer instead of an array, will this code still work?
If he only passes integers you have not the second dimension in the array. Therefore you wouldn't need to do it twice. You'd have to check wether the array is three-dimensional. Could be done something like this: Array.isArray(arrToConvert[0][0]). When this is true you have the three dimensional array, otherwise a two dimensional one.

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.