Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按分组属性对对象数组排序_Javascript_Arrays_Sorting - Fatal编程技术网

Javascript 按分组属性对对象数组排序

Javascript 按分组属性对对象数组排序,javascript,arrays,sorting,Javascript,Arrays,Sorting,我有以下对象数组: 更新:完整代码 export const fetchInvoices = function (firestore, currentUser) { const db = firestore .collection("customers") .doc(currentUser) .collection("subscriptions"); let invoices = [];

我有以下对象数组:

更新:完整代码

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;
}
我想按第一个索引上状态为“open”的索引对它们进行排序,然后按创建的
进行排序。有办法做到这一点吗

我一直在尝试使用
Object.values(obj).includes(“open”)
仅对
状态组进行排序:“open”
,以确定对象是否具有该值,然后按
真值进行排序。

但仍然无法实现“按组排序”。

您可以使用方法获取处于打开状态的对象,并使用如下方法对其进行排序

您可以通过在sort函数中相应地返回值来更改排序顺序

const数据=[
{
“计费原因”:“订阅创建”,
“已创建”:1614889156,
“货币”:“美元”,
“托管发票url”:“…”,
“id”:“…”,
“发票(pdf):“…”,
“编号”:“82190D09-0001”,
“期末”:1614889156,
“期初”:1614889156,
“状态”:“草稿”,
“小计”:3900,
“总数”:3 900
},
{
“计费原因”:“订阅周期”,
“已创建”:1614890009,
“货币”:“美元”,
“托管发票url”:“…”,
“id”:“…”,
“发票(pdf):“…”,
“编号”:“82190D09-0002”,
“期末”:1614890009,
“期初”:1614890009,
“状态”:“已支付”,
“小计”:3900,
“总数”:3 900
},
{
“计费原因”:“订阅更新”,
“已创建”:1614890064,
“货币”:“美元”,
“托管发票url”:“…”,
“id”:“…”,
“发票(pdf):“…”,
“编号”:“82190D09-0003”,
“期末”:1614890064,
“期初”:1614890009,
“状态”:“打开”,
“小计”:-1400,
“总数”:-1400
},
{
“计费原因”:“订阅创建”,
“已创建”:1614890802,
“货币”:“美元”,
“托管发票url”:“…”,
“id”:“…”,
“发票(pdf):“…”,
“编号”:“82190D09-0004”,
“周期结束”:1614890802,
“周期开始”:1614890802,
“状态”:“已支付”,
“小计”:3900,
“总数”:3 900
},
{
“计费原因”:“订阅创建”,
“已创建”:1614892003,
“货币”:“美元”,
“托管发票url”:“…”,
“id”:“…”,
“发票(pdf):“…”,
“编号”:“82190D09-0005”,
“期末”:1614892002,
“期初”:1614892002,
“状态”:“已支付”,
“小计”:3900,
“总数”:3 900
},
{
“计费原因”:“订阅创建”,
“已创建”:1614893124,
“货币”:“美元”,
“托管发票url”:“…”,
“id”:“…”,
“发票(pdf):“…”,
“编号”:“82190D09-0006”,
“期末”:1614893124,
“周期开始”:1614893124,
“状态”:“已支付”,
“小计”:3900,
“总数”:3 900
},
]
const open=data.filter(d=>d.status==“open”).sort((a,b)=>a.created-b.created);

控制台日志(打开)如果您愿意使用一些很棒的库,那么这个解决方案可能适合您使用,对于这种情况非常适合

假设将原始数组放入名为
data
的变量中,如下所示:

这将导致此输出:

{ draft:
   [ { billing_reason: 'subscription_create',
       created: 1614889156,
       currency: 'usd',
       hosted_invoice_url: '...',
       id: '...',
       invoice_pdf: '...',
       number: '82190D09-0001',
       period_end: 1614889156,
       period_start: 1614889156,
       status: 'draft',
       subtotal: 3900,
       total: 3900 } ],
  paid:
   [ { billing_reason: 'subscription_cycle',
       created: 1614890009,
       currency: 'usd',
       hosted_invoice_url: '...',
       id: '...',
       invoice_pdf: '...',
       number: '82190D09-0002',
       period_end: 1614890009,
       period_start: 1614890009,
       status: 'paid',
       subtotal: 3900,
       total: 3900 },
     { billing_reason: 'subscription_create',
       created: 1614890802,
       currency: 'usd',
       hosted_invoice_url: '...',
       id: '...',
       invoice_pdf: '...',
       number: '82190D09-0004',
       period_end: 1614890802,
       period_start: 1614890802,
       status: 'paid',
       subtotal: 3900,
       total: 3900 },
     { billing_reason: 'subscription_create',
       created: 1614892003,
       currency: 'usd',
       hosted_invoice_url: '...',
       id: '...',
       invoice_pdf: '...',
       number: '82190D09-0005',
       period_end: 1614892002,
       period_start: 1614892002,
       status: 'paid',
       subtotal: 3900,
       total: 3900 },
     { billing_reason: 'subscription_create',
       created: 1614893124,
       currency: 'usd',
       hosted_invoice_url: '...',
       id: '...',
       invoice_pdf: '...',
       number: '82190D09-0006',
       period_end: 1614893124,
       period_start: 1614893124,
       status: 'paid',
       subtotal: 3900,
       total: 3900 } ],
  open:
   [ { billing_reason: 'subscription_update',
       created: 1614890064,
       currency: 'usd',
       hosted_invoice_url: '...',
       id: '...',
       invoice_pdf: '...',
       number: '82190D09-0003',
       period_end: 1614890064,
       period_start: 1614890009,
       status: 'open',
       subtotal: -1400,
       total: -1400 } ] }

按两个不同的标准排序只是将您的标准与逻辑OR(| |)链接起来的问题

在第一个示例中,首先通过
data.status
将布尔值转换为整数,然后通过
data.created

const data=[{“已创建”:1,“状态”:“草稿”,},{“已创建”:5,“状态”:“已支付”,},{“已创建”:2,“状态”:“开放”,},{“已创建”:6,“状态”:“已支付”,},{“已创建”:3,“状态”:“开放”,},{“已创建”:4,“状态”:“已支付”,},];
const sortedData=数据
.sort((a,b)=>+(b.status=='open')-+(a.status=='open')| | a.created-b.created);
控制台日志(sortedData)

。作为控制台包装{max height:100%!important;top:0;}
按组划分,您是指数组的索引吗?像数组中[0]中的“status:open”和[1]中的“status:created”对象吗?@VinayKharayat假设有4个“status:open”对象,分别在[0]、[1]、[2]、[3]位置对它们进行排序。这看起来像是一个异步问题,排序之前是否尝试记录
发票
?我猜它是空的-您正在调用
invoices.sort(…
db.get
解析之前进行排序。请参阅:@pilchard I do,数组有数据,长度为9filter()的数组似乎没有过滤值“open”,控制台记录一个空数组
length:0
um,它与您在此处提供的数组配合良好,查看数组是否至少有一个状态为open的值这不是我要查找的结果,正如您在我提供的数组中所看到的,第一个索引[0]的状态为“draft”。然后是状态为“paid”,然后是“open”。我想要状态为的对象“开"要进入第一个索引0、1、2…如果可能存在多个索引。感谢您的回答,您提供的示例适用于您的对象数组,但不适用于我的对象数组,是否有可能因为不是硬代码数组而不起作用?我将更新完整的代码,以便您可以看到我是如何获取数组的如您在问题中提供的数组部分所预期的那样。请避免链接排序。您可以使用逻辑或在单个回调中链接增量。足够公平,已编辑。
{ draft:
   [ { billing_reason: 'subscription_create',
       created: 1614889156,
       currency: 'usd',
       hosted_invoice_url: '...',
       id: '...',
       invoice_pdf: '...',
       number: '82190D09-0001',
       period_end: 1614889156,
       period_start: 1614889156,
       status: 'draft',
       subtotal: 3900,
       total: 3900 } ],
  paid:
   [ { billing_reason: 'subscription_cycle',
       created: 1614890009,
       currency: 'usd',
       hosted_invoice_url: '...',
       id: '...',
       invoice_pdf: '...',
       number: '82190D09-0002',
       period_end: 1614890009,
       period_start: 1614890009,
       status: 'paid',
       subtotal: 3900,
       total: 3900 },
     { billing_reason: 'subscription_create',
       created: 1614890802,
       currency: 'usd',
       hosted_invoice_url: '...',
       id: '...',
       invoice_pdf: '...',
       number: '82190D09-0004',
       period_end: 1614890802,
       period_start: 1614890802,
       status: 'paid',
       subtotal: 3900,
       total: 3900 },
     { billing_reason: 'subscription_create',
       created: 1614892003,
       currency: 'usd',
       hosted_invoice_url: '...',
       id: '...',
       invoice_pdf: '...',
       number: '82190D09-0005',
       period_end: 1614892002,
       period_start: 1614892002,
       status: 'paid',
       subtotal: 3900,
       total: 3900 },
     { billing_reason: 'subscription_create',
       created: 1614893124,
       currency: 'usd',
       hosted_invoice_url: '...',
       id: '...',
       invoice_pdf: '...',
       number: '82190D09-0006',
       period_end: 1614893124,
       period_start: 1614893124,
       status: 'paid',
       subtotal: 3900,
       total: 3900 } ],
  open:
   [ { billing_reason: 'subscription_update',
       created: 1614890064,
       currency: 'usd',
       hosted_invoice_url: '...',
       id: '...',
       invoice_pdf: '...',
       number: '82190D09-0003',
       period_end: 1614890064,
       period_start: 1614890009,
       status: 'open',
       subtotal: -1400,
       total: -1400 } ] }