lodash sorting an array of objects in javascript -
i have array of objects response server, trying sort uid in ascending order, while keeping entire array intact data
array. meaning in data
array there 2 elements. trying @ uid in 1 of arrays in of elements within array , determine order of how elements should placed within data
array.
i have tried following using lodash, doesn't seem work, feel i'm getting close , assistance:
// sort by: console.log(res.results[0].data[0].row[1].uid) _.foreach(res.results, function(obj) { _.foreach(data, function(anotherobj) { _.foreach(row, function(row) { data.row = _.sortby(data.row, function(row) { return row[1].uid; }); }); }); });
here array of objects returned server:
var res = { results: [{ columns: ['large', 'medium', 'small'], data: [{ row: [{ sid: 13, tid: 427 }, { uid: 69, vid: 450, wid: 65 }, null], multirow: { nodule: [{ xid: '427', properties: { yid: 13, zid: 427 } }, { aid: '450', properties: { uid: 69, bid: 450, cid: 65 } }] } }, { row: [{ sid: 13, tid: 427 }, { vid: 432, uid: 65, wid: 61 }, null], multirow: { nodule: [{ xid: '427', properties: { yid: 13, zid: 427 } }, { aid: '432', properties: { bid: 432, uid: 65, cid: 61 } }] } }] }], errors: [] };
you don't need many loops:
_.foreach(res.results, function(result) { result.data = _.sortby(result.data, function(datum) { return datum.row[1].uid; }); });
you need iterate on results, each of might have data array going sort. sortby
callback takes each of elements of array , supposed return value compare.
Comments
Post a Comment