sorting - UnderscoreJs : objects sortBy with empty attributes -


problem is, want sort price array of users might contains empty attributes one:

var array =  [     {       "created": "2015-11-27t16:33:46.781z",       "name": "johan",       "pricing": {         "base_price" : "12",         "price_by_hour" : "5"       }     },     {       "created": "2015-11-27t16:33:46.781z",       "name": "marco"     },     {       "created": "2015-11-27t16:33:46.781z",       "name": "jane",       "pricing": {         "base_price" : "8",         "price_by_hour" : "11"       }     }  ];  array = _.sortby(array, function(item) {     return item.pricing.base_price; }); 

console.log(array);

typeerror: cannot read property 'base_price' of undefined 

how can put items without pricing object @ bottom of list , still sorting it?

in case, want sort list jane first, johan, marco.

here's easy way it:

_.sortby(array, 'pricing.base_price'); 

when pass string iteratee sortby(), property() function used. function works property paths , returns undefined if property doesn't exist.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -