Mapping an array of objects to key/value pairs in CoffeeScript

I have an array of elements that I would like to apply a mapping to to convert it into key value pairs on a single object (to mimic an associative array).

The approach in Can destructuring assignment be used to effect a projection in CoffeeScript? does not seem to work for me as it results in a simple array instead of key/value pairs.

My language of choice is CoffeeScript or JavaScript.

An example:

[{name: 'a', value: 'b', other: 'c'}, {name: 'd', value: 'e', other: 'f'}]

is supposed to be transformed into:

{
  a: 'b',
  d: 'e'
}

One-liners are preferred. ;-)

Answers


To fix the syntax error, you'll have to expand { @name: @value } to:

o = {}; o[@name] = @value; o

You can then merge the objects with $.extend() and a splat (with the empty object to avoid accidentally extending jQuery):

$.extend {}, $(row).children('input').map(() -> o = {}; o[@name] = @value; o)...

Though, a simpler option would be just to use a 2-liner:

result = {}
$(row).children('input').each(() -> result[@name] = @value)

var arr = [{name: 'a', value: 'b', other: 'c'}, {name: 'd', value: 'e', other: 'f'}];

var obj = arr.reduce(function ( total, current ) {
    total[ current.name ] = current.value;
    return total;
}, {});

Pure javascript. It's practically a one liner, and it looks hawt.

Array.prototype.reduce is ES5, but isn't difficult to shim. Here's an example shim:

Array.prototype.reduce = function ( fun, initVal ) {
    var sum = initVal || this[ 0 ],
        i = 1, len = this.length;

    do {
        sum = fun.call( undefined, sum, this[i], i, this );
    } while ( ++i < len );

    return sum;
};

arr.reduce is a sophisticated version of arr.map, which is a sophisticated version of arr.forEach. You can do this for the same effect:

var obj = {};
arr.forEach(function ( val ) {
    obj[ val.name ] = val.value;
});

//and using jQuery.each
var obj = {};
$.each( arr, function ( index, val ) {
    obj[ val.name ] = val.value;
});

//latter version in coffeescript:
obj = {}
$.each( arr, (index, val) ->
    obj[ val.name ] = val.value
)

values = {}
values[name] = value for {name, value} in arr

or in javascript:

var values = {}
arr.forEach(function(o){
    values[o.name] = o.value
})

Which is almost exactly what the CoffeeScript one compiles to.


Need Your Help

Is there a compiler or IDE for C on Windows that's regarded as an industry standard?

c windows ide compiler-construction

Taking advice from this post, I purchased a copy of 'The C Programming Language' and am happily reading my way through.

C++ - calling a method from a class template

c++ templates class hashtable functor

I'm currently having a problem with a class template in C++. I'm currently making a hash table.

Keeping webservice alive if a spawned thread is still working

.net multithreading web-services application-pool

I have a webservice that will be called by a nightly job process documents, each document will be queued and executed on it's own background processor. This process can take a couple hours or a few

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.