0

I would like to convert an array like this :

url : Array

0:"michael"
1:"jason"

url : Array

0:Array
 0:"michael"
1:Array
 0:"jason"

I need it for add data to (jquery) datatable ("aaData": exceptions,)

3
  • Sorry it makes no sense what you posted Commented Feb 13, 2014 at 16:35
  • 1
    So you want to convert an array into an array(of the same length) which has a subarray of the same values? Unless you want to add objects inside your array, I don't see any point in doing that. But if you want to do, use the map function over the arr and return [data] (assuming data is the function parameter) Commented Feb 13, 2014 at 16:37
  • I have an string with this value url="michael,jason"; i've splited this values with var result= url.split(";"); now i have result = ["michael","jason"] but i need somenthing like result =[["michael"],["jason"]]; I need it to add this information to datatable "aaData" Commented Feb 13, 2014 at 16:44

1 Answer 1

1

In JavaScript the Array is this: ["michael", "jason"]

This is an Object: {0: "michael", 1: "jason"}

To convert Array as you wrote you can use script like this:

var a = ["michael", "jason"];
for (var i=0; i<a.length; i++) {
    a[i] = [a[i]];
}

To convert Object as you wrote you can use script like this:

var obj = {0: "michael", 1: "jason"};
for (var i in obj) { // here is a difference - using `in` keyword
    obj[i] = [obj[i]];
}

Here is a working fiddle.

UPDATE:

For modern browsers (IE9+, FF1.5+) you can use map method of Array object:

var a = ["michael", "jason"];
a = a.map(function(item) {
    return [item];
});
Sign up to request clarification or add additional context in comments.

Comments

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.