user3241620
user3241620

Reputation: 95

Transform Array from one row Array to multiple row array

i have an array that returns the values like this:

0: 1,2,3,4

i need it to return array values like this:

0: 1
1: 2
2: 3
3: 4

If im doing it with javascript, how can i go about it?

Upvotes: 1

Views: 653

Answers (3)

gurvinder372
gurvinder372

Reputation: 68393

i have an array that returns the values like this:

0: 1,2,3,4

try this

"0: 1,2,3,4".split(":").pop().split(",").map( function(value){return [value]} );

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386600

With a loop, eg.

var object = { '0': [1, 2, 3, 4] },       // the object
    result = function (o) {               // the iife for the result
        var r = {};                       // the temporary variable
        o['0'].forEach(function (a, i) {  // the loop over the property zero
            r[i] = a;                     // the assignment to the object with
        });                               // the wanted key
        return r;                         // the return of the temporary object
    }(object);                            // the start with the object

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

Upvotes: 1

vvo
vvo

Reputation: 2883

You could do this:

var array = ['1,2,3,4'];
console.log(array[0].split(','));

Upvotes: 1

Related Questions