我有一個集合,像這樣:重新排序陣列基於條件lodash
var scores = [{
name: foo,
score: 34
}, {
name: bar,
score: 23
}, {
name: baz,
score: 99
}]
我還具有包含名稱的變量:
var selfName = 'bar';
我怎樣才能重新排列分數集,使得包含該對象selfName位於索引0預期結果:
[{
name: bar,
score: 23
}, {
name: foo,
score: 34
}, {
name: baz,
score: 99
}]
我知道我可以做到這一點:
var isSelfName = (selfName, score) => {
var _name = selfName;
return (score) => {
return score.name === selfName;
}
};
var result = _.filter(scores, isSelfName(selfName));
result.push(_.reject(scores, isSelfName(selfName)));
result = _.flatten(result);
console.log('result:', result);
有沒有更好的方法呢?
並添加ES6傳播經營者,使之更加簡潔: VAR的結果= _.concat(...溫度); – jgr0
@ jgr0謝謝指出。更新。 – WitVault