Reorder objects

What is the best way to order and transform:

{
  '32': 'foo',
  '24': 'bar',
  '36': 'doe'
}

into:

[
  {'24': 'bar'},
  {'32': 'foo'},
  {'36': 'doe'}
]

I need to order them based on key, which is a string in the original object. jQuery’s API is allowed to use.

Answers


Try this:

function arrayMe(obj){
    var indexes = [];
    for(index in obj){
        indexes.push(index);
    }
    indexes.sort();
    var return_array = [];
    for(var i = 0; i < indexes.length; i++){
        return_array[i] = {};
        return_array[i][indexes[i]] = obj[indexes[i]];
    }
    return return_array;
}

All you would have to do is:

arrayMe(oldObject);

Fiddle: http://jsfiddle.net/maniator/uBqjt/


Need Your Help

JMS Prefetch + Cached Consumers + Stuck Messages

java spring jms activemq spring-jms

I am trying to get JMS Consumers to work towards an ActiveMQ server however I am running into some issues which solution seem to just cause more problems.

Pass a block to a C++ method from objective C

c++ objective-c ios4 objective-c-blocks

I have a C++ helper class that I use with objective-C. I would like to pass the c++ class a block from a view controller (a callback) so that when it is executed I am on the main thread and can upd...

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.