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?

9
  • 2
    Convert your object into an array. Objects have no order and cannot be sorted, so this is a necessary step anyway. Commented Sep 8, 2016 at 9:54
  • @deceze Would you recommend most practical method to convert this object into proper sortable array? Commented Sep 8, 2016 at 9:55
  • 1
    stackoverflow.com/questions/6857468/… This website is full of ressources ! Commented Sep 8, 2016 at 9:56
  • 1
    Why is it an object to begin with? Can you change something at the server to give you a plain array? Commented Sep 8, 2016 at 9: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 Commented Sep 8, 2016 at 9:56

2 Answers 2

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: [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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for noticing it. It is dummy info now, but even tho, all of these data is public anyway.
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.

1 Comment

I prefer not to use lodash at this time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.