Kunok
Kunok

Reputation: 8759

Sort JSON data object of objects by object value

I fetched data from mongoDB and sent it to client side, JSON returned is structured like this:

{ "0" : { "_id" : { "$oid" : "57d129bfaa1a7567d9d" }, "id" : "155edc1698efc18e", "history_id" : "41752", "messages" : [ { "id" : "155edc1698efc18e", "snippet" : "Mari567567rnar | IT U756567ive travel experience T: +35667575756756| T: +7567560 F: +3567756792 | E: ad75675travel.com Ulix doo | Miramarsk765657 HR–105675 Zagreb ----------", "labels" : [ "INBOX", "IMPORTANT", "CATEGORY_PERSONAL", "Label_15", "Label_7" ], "date" : "1468572702000", "history_id" : "41752",

I parsed it using var objects = JSON.parse(response);

When I log it, it looks like this:

enter image description here

It is basically an object of objects. It is acting like array but it is not array and I can't use array methods such as sort.

This is how my inner objects looks like.

{_id:object, history_id:string, id:string, messages:array}

I am about to convert history_id value into int and sort these objects by that value.

I am looking for the best method to sort these objects by that value. I went with .sort method but it is array method and it is not defined for object type. I would convert the main object into array and call that method. Or else, if there is a way to sort these objects in a more elegant way, without converting it into array. Which method is more practical and how to implement it properly?

Upvotes: 0

Views: 99

Answers (2)

Alador
Alador

Reputation: 1

As said in comments you have to work with an array to sort the elements.

You can do that with lodash library this way :

var arr = _.values(obj);

If needed here is the lodash values function documentation

When it's done you can use the array.sort method to sort your data using a compareFunction parameter as you can see here.

Upvotes: 0

Icepickle
Icepickle

Reputation: 12796

If you want to, you could use the Object.keys( objects ) that will return you an array for all the keys in your originally parsed object, which you could then iterate and transform into an array

A bit like this example

'use strict';
var fakeJson = '{ "0" : { "_id" : { "$oid" : "57d129bfaa1ab0795a42cd9d" }, "id" : "155edc1698efc18e", "history_id" : "41752", "messages" : [ { "id" : "155edc1698efc18e", "snippet" : "Marino Pernar | IT Ulix exclusive travel experience T: +385 1 640 6649 | T: +385 91 6410 930 F: +385 1 615 4092 | E: [email protected] Ulix doo | Miramarska cesta 26 | HR–10000 Zagreb ----------", "labels" : [ "INBOX", "IMPORTANT", "CATEGORY_PERSONAL", "Label_15", "Label_7" ], "date" : "1468572702000", "history_id" : "41752" } ] }, "1": { "_id" : { "$oid" : "57d129bfaa1ab0795a42cd9d" }, "id" : "155edc1698efc18e", "history_id" : "41750", "messages" : [ { "id" : "155edc1698efc18e", "snippet" : "Marino Pernar | IT Ulix exclusive travel experience T: +385 1 640 6649 | T: +385 91 6410 930 F: +385 1 615 4092 | E: [email protected] Ulix doo | Miramarska cesta 26 | HR–10000 Zagreb ----------", "labels" : [ "INBOX", "IMPORTANT", "CATEGORY_PERSONAL", "Label_15", "Label_7" ], "date" : "1468572702000", "history_id" : "41752" } ] } }';

var objects = JSON.parse(fakeJson);

console.log(objects);

var copy = [];

Object.keys(objects).forEach(function(key) {
  copy.push( objects[key] );
});

copy.sort(function(a, b) {
  return a.history_id - b.history_id;
});

console.log(copy);

btw, are you sure you want to dispense the personal information (email address, telephone nrs) on here? I hope it's dummy info.

Upvotes: 1

Related Questions