angularjs - Sorting an array by its nth-grand-child -



angularjs - Sorting an array by its nth-grand-child -

i want extend functionality of function angularjs sorting property.

it sorts array kid property. want create more generalized, create able sort specified nth-grand-child.

the function

function orderobjectby(input, attribute) { if (!angular.isobject(input)) homecoming input; var array = []; for(var objectkey in input) { array.push(input[objectkey]); } array.sort(function(a, b){ = parseint(a[attribute]); b = parseint(b[attribute]); homecoming - b; }); homecoming array; }

here's array works this: orderobjectby(object, 'stat_a')

array = [ {stat_a:3}, {stat_a:2}, {stat_a:5}, ]

here's want work it: orderobjectby(object, 'stats.a')

array = [ {stats:{a: 3}}, {stats:{a: 2}}, {stats:{a: 5}}, ]

i have no thought how though. line compares

a = parseint(a[attribute]);

cannot take stats.a attribute.

neither figure out how create parseint(a[stats][a])

note, stats.a illustration simply .split('.').pop() etc want more general approach, that'll work stats.some.things.a or stats.some.other.things.a ...)

any guidance how proceed?

split attribute name "." , explore object attribute name list.

array.sort(function(a,b) { = parseint(getattribute(a, attribute); b = parseint(getattribute(b, attribute); homecoming a-b }) function getattribute(object,attribute) { var o = object; var attrs = attribute.split(".") ( var i=0; i<attrs.length; i++) { o = o[attrs[i]]; if (o==null) { homecoming null; } } homecoming o; }

arrays angularjs sorting

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -