1

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?

1
  • Input is array([1,2,3,4]) or object({0:[1,2,3,4]}) ? Commented Mar 4, 2016 at 11:21

3 Answers 3

1

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>');

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

Comments

1

You could do this:

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

2 Comments

i get this error:Uncaught TypeError: sortOrderValues[0].split is not a function
Can you do console.log(sortOrderValues) and give me the output, thanks
1
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]} );

2 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
@DanielCheung check now

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.