let _ = require('lodash');
let arr = [
{
id: 1,
index: 0,
},
{
id: 2,
index: 1,
position: 0,
},
{
id: 3,
index: 2,
}
];
let sortFn = (o) => {
if(o.position === undefined) return o.index;
return o.position;
};
let result = _.sortBy(arr, sortFn)
.map(o => o.id)
// Expected result: id: 2, 1, 3
console.log(result);
let arr2 = [
{
id: 1,
index: 0,
position: 2,
},
{
id: 2,
index: 1,
position: 0,
},
{
id: 3,
index: 2,
}
];
let result2 = _.sortBy(arr2, sortFn)
.map(o => o.id)
// Expected result: id: 2, 3, 1
console.log(result2);