0

I'm getting an object of objects from Firebase, but need to change the default items order. For that, I use Lodash orderBy() method.

computed: {
  sortedLogs() {
    return orderBy(this.logs, 'date', 'desc')
  }
},

The problem now is this method remove objects keys, and I need these keys to manage item delete request.

Original

{
    MFVMHJHnbpr: {
        date: 1598270895
        side: 'l'
    },
    MFVMblsdfdPb: {
        date: 1598270825
        side: 'r'
    },
    MsxvblsdfdPb: {
        date: 1598271225
        side: 'l'
    },
}

After orderBy()

{
    0: {
        date: 1598270895
        side: 'l'
    },
    1: {
        date: 1598270825
        side: 'r'
    },
    2: {
        date: 1598271225
        side: 'l'
    },
}
marcelo2605
  • 2,734
  • 4
  • 29
  • 55
  • which keys? and how does the object look before et after applying that function? – Boussadjra Brahim Aug 24 '20 at 18:18
  • 1
    Could you provide some sample data? I. e. what is in `this.logs`? An SO question should always contain a detailed description of the problem: What do you want to achieve (output) on the basis of which input? – Carsten Massmann Aug 24 '20 at 18:19
  • @BoussadjraBrahim the key generated by Firebase for each entry in the database. After run the orderBy() method, the keys are replaced by an index sequence. – marcelo2605 Aug 24 '20 at 18:20
  • 1
    and how does the object look before et after applying that function? – Boussadjra Brahim Aug 24 '20 at 18:20
  • @cars10m `this.logs` is a Vue data where I stored my Firebase return. It's a object of objects. – marcelo2605 Aug 24 '20 at 18:21
  • Is there no way you could list this "object of objects" here? Albeit in a simplified form? An [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) would be nice. – Carsten Massmann Aug 24 '20 at 18:23
  • 1
    check this, this might help: https://stackoverflow.com/questions/43112327/sorting-a-map-of-keys-and-values-please-to-order-a-list-in-es6-with-lodash-keep – Syed Aug 24 '20 at 18:26

2 Answers2

1

You can do it in plain JavaScript too, but if you want to rely on the order of elements you will have to convert the object to an array. Objects are not guaranteed to have a prescribed order in JavaScript (see here).

const inp= {
    MFVMHJHnbpr: {
        date: 1598270895,
        side: 'l'
    },
    MFVMblsdfdPb: {
        date: 1598270825,
        side: 'r'
    },
    MsxvblsdfdPb: {
        date: 1598271225,
        side: 'l'
    }
};
const out=Object.entries(inp).map(o=>(o[1].key=o[0],o[1])).sort((a,b)=>a.date-b.date)
console.log('Array:',out);

// alternatively, creating a "sorted object" - no guarantee!

const outObj=out.reduce((a,c)=>{let o={...c}; delete o.key; a[c.key]=o; return a;},{})
console.log('Object:',outObj);
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
0

orderBy does not mutate the original object, so you can just store a copy of this.logs and use that for deletion. This means, of course, you must also update the sortedLogs after you change the original data.

tay_thomp
  • 245
  • 2
  • 12