0

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?

Kunok
  • 8,089
  • 8
  • 48
  • 89
  • 2
    Convert your object into an array. Objects have no order and cannot be sorted, so this is a necessary step anyway. – deceze Sep 08 '16 at 09:54
  • @deceze Would you recommend most practical method to convert this object into proper sortable array? – Kunok Sep 08 '16 at 09:55
  • 1
    http://stackoverflow.com/questions/6857468/converting-a-js-object-to-an-array This website is full of ressources ! – Alburkerk Sep 08 '16 at 09:56
  • 1
    Why is it an object to begin with? Can you change something at the server to give you a plain array? – deceze Sep 08 '16 at 09:56
  • if it has a length property, you could use `Array.prototype.sort.call( objectName, function(a, b) { return a.history_id - b.history_id; } )` or something similar – Icepickle Sep 08 '16 at 09:56
  • @deceze A good question. It is using PHP backend, maybe PHP JSON encoding method is returning weird output. I never had the issues with JSON when I used other languages in the backend. – Kunok Sep 08 '16 at 09:57
  • Your actual problem is probably this: http://stackoverflow.com/q/27232834/476 – deceze Sep 08 '16 at 10:01
  • You may take a look at https://lodash.com/docs. This might help you for manipulation. – Rajeshwar Sep 08 '16 at 10:01
  • @Rajeshwar Thanks for advice but I prefer to not use lodash library at this moment. I am trying to go as lightweight as possible. – Kunok Sep 08 '16 at 10:02

2 Answers2

1

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: admin@ulixtravel.com 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: admin@ulixtravel.com 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.

Icepickle
  • 12,689
  • 3
  • 34
  • 48
  • Thanks for noticing it. It is dummy info now, but even tho, all of these data is public anyway. – Kunok Sep 08 '16 at 10:15
0

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.

Alador
  • 1