You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于对象数组中连续日期的comment分类存储issueId

Solution to Identify Risk and Incident Issues Based on Consecutive Comments

Got it, let's break down how to tackle this problem step by step. We'll process the server's returned object array to populate the arrRisk and arrIncident arrays exactly as you specified. Here's a practical, easy-to-follow approach:

Step 1: Group Data by issueId

First, we need to cluster all objects by their issueId—this lets us analyze each issue's comment history independently without mixing data across different issues. We'll use Array.reduce() for this grouping:

// Example server response data (swap this with your actual dataset)
const serverResponse = [
  { issueId: 1001, date: '2024-05-20', comment: 'Awaiting approval' },
  { issueId: 1001, date: '2024-05-21', comment: 'Awaiting approval' },
  { issueId: 1001, date: '2024-05-22', comment: 'Awaiting approval' },
  { issueId: 1002, date: '2024-05-20', comment: 'In progress' },
  { issueId: 1002, date: '2024-05-21', comment: 'In progress' },
  { issueId: 1007, date: '2024-05-20', comment: 'Resolved' },
  { issueId: 1007, date: '2024-05-21', comment: 'Closed' }
];

// Group entries by issueId
const groupedIssues = serverResponse.reduce((acc, item) => {
  if (!acc[item.issueId]) {
    acc[item.issueId] = [];
  }
  acc[item.issueId].push(item);
  return acc;
}, {});

Step 2: Sort Each Issue's Entries by Date

To accurately check consecutive days, we need each issue's entries ordered chronologically. Convert date strings to Date objects for reliable sorting:

// Sort each issue's entries from earliest to latest date
Object.values(groupedIssues).forEach(entries => {
  entries.sort((a, b) => new Date(a.date) - new Date(b.date));
});

Step 3: Detect Consecutive Comments and Populate Arrays

Now we'll loop through each grouped issue, track how many days in a row the comment stays the same, and add issueIds to the correct arrays. We use Set to avoid duplicate entries (e.g., an issue with 4 consecutive same comments won't get added to arrRisk multiple times):

const arrRisk = new Set();
const arrIncident = new Set();

// Check each issue's comment history
Object.entries(groupedIssues).forEach(([issueId, entries]) => {
  let consecutiveSameComments = 1; // Start with 1 since we compare current to previous

  for (let i = 1; i < entries.length; i++) {
    const currentEntry = entries[i];
    const prevEntry = entries[i - 1];

    if (currentEntry.comment === prevEntry.comment) {
      consecutiveSameComments++;
      // Add to arrRisk if 2 consecutive days (today + yesterday)
      if (consecutiveSameComments === 2) {
        arrRisk.add(issueId);
      }
      // Add to arrIncident if 3+ consecutive days
      if (consecutiveSameComments >= 3) {
        arrIncident.add(issueId);
      }
    } else {
      // Reset count if comments differ
      consecutiveSameComments = 1;
    }
  }
});

// Convert Sets to regular arrays if needed
const finalArrRisk = Array.from(arrRisk);
const finalArrIncident = Array.from(arrIncident);

// Example output:
// finalArrRisk = ["1001", "1002"]
// finalArrIncident = ["1001"]
// issueId 1007 is excluded since its comments changed between days

Quick Notes

  • Make sure your date strings are in a format new Date() can parse (like YYYY-MM-DD). If not, adjust the sorting logic to match your date format.
  • Using Set ensures each issueId only appears once in each array, even if it meets the condition multiple times.
  • This logic works for any number of entries per issue—whether it's 2 days or 10 days of history.

内容的提问来源于stack exchange,提问作者WannaBeBetter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:31:55