jQuery - change a list of elements to an associative array

Posted by Brian M. Hunt on Stack Overflow See other posts from Stack Overflow or by Brian M. Hunt
Published on 2010-06-07T21:40:12Z Indexed on 2010/06/07 21:42 UTC
Read the original article Hit count: 400

Filed under:
|
|
|
|

Given an associative array (of the sort returned by jQuery.serializeArray()) like this:

[
{ 'name': 'abc', 'value': 'aaa', '__proto__': [Object] },
{ 'name': 'def', 'value': 'bbb', '__proto__': [Object] },
{ 'name': 'abc', 'value': 'ccc', '__proto__': [Object] }
]

How can one convert this, using either jQuery or just javascript, to an associative array of name: [values] like this:

{
   'abc': ['aaa', 'ccc'],
   'def': ['bbb']
}

This seems to essentially be the inverse of this question: Build associative array based on values of another associative array... but in Javascript (not PHP). I wasn't able to find this question on Stackoverflow, though I thought it would have been asked.

Thank you for reading.

Brian

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery