Tuesday, May 7, 2024
 Popular · Latest · Hot · Upcoming
61
rated 0 times [  67] [ 6]  / answers: 1 / hits: 46361  / 15 Years ago, mon, july 13, 2009, 12:00:00

I have:



var keys = [ height, width ];
var values = [ 12px, 24px ];


And I'd like to convert it into this object:



{ height: 12px, width: 24px }


In Python, there's the simple idiom dict(zip(keys,values)). Is there something similar in jQuery or plain JavaScript, or do I have to do this the long way?


More From » json

 Answers
18

Simple JS function would be:



function toObject(names, values) {
var result = {};
for (var i = 0; i < names.length; i++)
result[names[i]] = values[i];
return result;
}


Of course you could also actually implement functions like zip, etc as JS supports higher order types which make these functional-language-isms easy :D


[#99139] Wednesday, July 8, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lonnier

Total Points: 621
Total Questions: 113
Total Answers: 98

Location: Nepal
Member since Mon, Jan 4, 2021
3 Years ago
;