I have the following array of objects:
Update: full code
export const fetchInvoices = function (firestore, currentUser) {
const db = firestore
.collection("customers")
.doc(currentUser)
.collection("subscriptions");
let invoices = [];
db.get().then((subscriptionSnapshot) => {
subscriptionSnapshot.forEach((doc) => {
doc.ref.collection("invoices").get().then((invoiceSnapshot) => {
invoiceSnapshot.forEach(async (doc) => {
let invoice = {
"billing_reason": doc.data().billing_reason,
"created": doc.data().created,
"currency": doc.data().currency,
"hosted_invoice_url": doc.data().hosted_invoice_url,
"id": doc.id,
"invoice_pdf": doc.data().invoice_pdf,
"number": doc.data().number,
"period_end": doc.data().period_end,
"period_start": doc.data().period_start,
"status": doc.data().status,
"subtotal": doc.data().subtotal,
"total": doc.data().total
};
Object.defineProperty(invoice, 'invoice_id', { value: doc.id });
invoices.push(invoice);
})
})
});
});
const sortedData = invoices.sort((a, b) => +(b.status === 'open') - +(a.status === 'open'));
console.log(sortedData);
return sortedData;
}
I want to sort them by the ones with status: "open" on the first indexes, and then sort by created. Is there a way to do this?
I've been trying to sort only the group of status: "open" using Object.values(obj).includes("open") to determine if the object has the value and then sort by truth as here.
But still can't even make this "sort by group".
invoicesbefore sorting? I'm guessing it's empty - you're callinginvoices.sort(...before thedb.getresolves. see: How do I return the response from an asynchronous call?