Javascript Array of Objects Sorting by Key

> var input = [];
> input.push({key:’z’, value:’zzz’});
> input.push({key:’a’, value:’aaa’});
> input.push({key:’b’, value:’bbb’});
> input.push({key:’x’, value:’xxx’});
> input.push({key:’m’, value:’mmm’});

> JSON.stringify(input);
“[{“key”:”z”,”value”:”zzz”},{“key”:”a”,”value”:”aaa”},{“key”:”b”,”value”:”bbb”},{“key”:”x”,”value”:”xxx”},{“key”:”m”,”value”:”mmm”}]”

> var output = _.object(_.map(input, function(item){ return item.key}), _.map(input, function(item){return item.value}));
> output
a:”aaa”
b:”bbb”
m:”mmm”
x:”xxx”
z:”zzz”

Leave a Reply

Your email address will not be published. Required fields are marked *

59 − = 54