Change array to object using javascript

时间:2015-08-07 02:14:40

标签: javascript arrays object

I need to change an array into an object using javascript

For an example. I have an array which looks like :

colours [        
["black","red",shades : ["0","1","2"]]        
]

I want to change this to :

colours [
    {
    "black","red", shades : ["0","1","2"]
    }
]

Can anyone please tell me how I can do this using javascript code.

2 个答案:

答案 0 :(得分:1)

var arr = $.map(obj, function(el) { return el; });

Fiddle

This question is been asked.

Here's the full details

答案 1 :(得分:1)

function toObject(arr) {
 var rv = {};
 for (var i = 0; i < arr.length; ++i)
  rv[i] = arr[i];
 return rv;
}

Try to read this