Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

Sort array of JavaScript objects by property value


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I just went through lodash doc, and perhaps you could try sortBy

Try it: http://jsfiddle.net/3Wza8/

var myObjects = [
    { id: '1', username: 'bill.jones', active: true, createdon: new Date('03/29/2014') },
    { id: '2', username: 'woohoo', active: true, createdon: new Date('03/28/2014') },
    { id: '3', username: 'someuser', active: true, createdon: new Date('03/30/2014') }
];

myObjects = _.sortBy(myObjects, 'createdon');

_.forEach(myObjects, function (result) {
    console.log(result);
});

EDIT: as Cookie Monster pointed out, it's important that your createdon field is a Date, and not a string.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...