Transforming a 2-element Array (from formContents) into an object

17 views
Skip to first unread message

paftek

unread,
Feb 20, 2007, 11:42:38 AM2/20/07
to MochiKit
Hello there,

I need advice from experts ! What is the best way to convert the
output of DOM.formContents() ( an array like : [ ['name1', 'name2',
'nameN'], ['value1', 'value2', 'valueN'] ] ) into an object like :
{ 'name1': 'value1', 'name2': 'value2', 'name3': 'value3'} ?

The problem is easy to solve, but I am just wondering if MochiKit
offers a quick and elegant answer to that (for instance, a kind of
opposite to Base.items() ).

Thanks,

paf

Aaron Faanes

unread,
Feb 20, 2007, 2:35:10 PM2/20/07
to MochiKit
MochiKit.Iter.izip(array[0], array[1])

will give you what you want.

- Aaron

Aaron Faanes

unread,
Feb 20, 2007, 2:39:34 PM2/20/07
to MochiKit
Er, I'm sorry, it will almost give you what you want. It wont wrap it
up in an object, though. It'll just be [ [array[0][0], array[1][0]],
[array[0][1], array[1][1] ] ... ]

Which doesn't really get you anywhere, so I'm thinking you'd just do
it inline.

Sorry about that.

- Aaron

paftek

unread,
Feb 22, 2007, 9:32:16 AM2/22/07
to MochiKit
Thanks for the anwers. I have finally opted for :

var data = formContents(foo);
var names = data[0];
var values = data[1];
var nbFields = names.length;
for(var i = 0; i < nbFields; i++) {
params[names[i]] = values[i];
}

Wow, difficult ! =)

paf

Reply all
Reply to author
Forward
0 new messages