ยังไงเพื่อนแผนที่อาเรย์ส่วนประกอบในจาวาสคริปต์

0

คำถาม

เพียงส่วนประกอบนั่นมีค่ามากกว่าหรือเท่ากับประตูนั่นต้องเป็นยังอยู่ในอาเรย์. งั้นคนใหม่อาเรย์จะต้องถูกสร้างขึ้นซึ่งจะมีหลายสิ่ง. ทุกสิ่งจะต้องสองคนคุณสมบัติของคนเริ่มและท้ายที่สุด

ถ้ามันมีหลายส่วนประกอบอยู่แถว(ซึ่งมีวันที่และเวลา 10 นาทีห่างกัน)พวกเขาจะถูกจัดกลุ่มได้อยู่ในที่เดียวกับวัตถุ. อยู่ที่ไหนค่าเริ่มต้นจะเป็นวันที่และเวลาของธาตุก่อนและจุดจบค่าจะเป็นค่าวันที่และเวลาของธาตุสุดท้ายของกลุ่มอีก 10 มิน

ถ้ามันไม่มาหลายส่วนประกอบตามที่ค่าเริ่มต้นจะเป็นวันที่และเวลาและจุดจบจะเป็นวันที่และเวลาอีก 10 นาที

const data = [{
    timestamp: '2021-11-23T14:00:00+0000',
    amount: 21
  },
  {
    timestamp: '2021-11-23T14:10:00+0000',
    amount: 27
  },
  {
    timestamp: '2021-11-23T14:20:00+0000',
    amount: 31
  },
  {
    timestamp: '2021-11-23T14:30:00+0000',
    amount: 29
  },
  {
    timestamp: '2021-11-23T14:40:00+0000',
    amount: 18
  },
  {
    timestamp: '2021-11-23T14:50:00+0000',
    amount: 17
  },
  {
    timestamp: '2021-11-23T15:00:00+0000',
    amount: 25
  },
  {
    timestamp: '2021-11-23T15:10:00+0000',
    amount: 21
  }
]

const threshold = 25
const aboveThreshold = data.filter(element => element.amount >= threshold)
const workSchedule = []

for (let i = 0; i < aboveThreshold.length; i++) {
  if (i === 0) {
    workSchedule.push({
      start: aboveThreshold[i].timestamp,
      end: aboveThreshold[i + 1].timestamp
    })
  }
  if (i > 0 && i < aboveThreshold.length - 1) {
    if (aboveThreshold[i].timestamp.slice(11, 13) === aboveThreshold[i + 1].timestamp.slice(11, 13)) {
      workSchedule.push({
        start: aboveThreshold[i].timestamp,
        end: aboveThreshold[i + 1].timestamp
      })
    }
  }
  if (i === aboveThreshold.length - 1) {
    workSchedule.push({
      start: aboveThreshold[i].timestamp,
      end: aboveThreshold[i].timestamp
    })
  }
}

console.log(workSchedule)

แต่ผลสุดท้ายก็จบลงฉันต้องการต่อไปนี้:

[
    {
        start: '2021-11-23T14:10:00+0000',
        end: '2021-11-23T14:40:00+0000'
    },
    {
        start: '2021-11-23T15:00:00+0000',
        end: '2021-11-23T15:10:00+0000'
    }
]

ฉันหวังว่าผมชัดเจน

arrays javascript typescript
2021-11-23 20:24:54
1

คำตอบที่ดีที่สุด

1

คุณสามารถปรับใช้ง่ายๆลดฟังก์ชันจะได้ผลคุณต้องการกับความช่วยเหลือนิดหน่ออกจาก Date วัตถุ. นี่คือทางออก:

const aboveThreshold = data.filter(element => element.amount >= threshold);

const nws = aboveThreshold.reduce((acc, v) => {
  const end = new Date(Date.parse(v.timestamp) + 600000);
  if (acc.length === 0) return [{ start: v.timestamp, end: end.toISOString() }];
  let diff = Date.parse(v.timestamp) - Date.parse(acc[acc.length - 1].end);
  // checks if the difference is less than 10 minutes
  if (diff <= 10 * 60 * 1000) {
    acc[acc.length - 1].end = end.toISOString();
  } else {
    acc.push({ start: v.timestamp, end: end.toISOString() });
  }
  return acc
}, []);

ตรวจสอ ลดเอกสาร.

นี่คือผลลัพธ์มันให้กับข้อมูลของคุณ

[{
  end: "2021-11-23T14:40:00.000Z",
  start: "2021-11-23T14:10:00+0000"
}, {
  end: "2021-11-23T15:10:00.000Z",
  start: "2021-11-23T15:00:00+0000"
}]
2021-11-23 22:14:24

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่