简体   繁体   English

如何从数组中的 object 中删除相同的日期字符串?

[英]How to remove same date string from object inside an array?

Hello I have a array like this.你好,我有一个这样的数组。

const array = [
{date: "2022-02-22 18:00:00"}
{date: "2022-02-22 17:00:00"}
{date: "2022-02-22 18:00:00"}
{date: "2022-02-23 01:00:00"}
{date: "2022-02-23 10:00:00"}
{date: "2022-02-24 18:00:00"}
]

Here I have to keep only one object for each day.在这里,我每天只需要保留一个 object。 Here It should be like that-这里应该是这样的——

[
{date: "2022-02-22 18:00:00"}
{date: "2022-02-23 01:00:00"}
{date: "2022-02-24 18:00:00"}
]

Here you can see 22, 23, 24 date object. Not same date twice.在这里您可以看到 22、23、24 日期 object。两次不同的日期。 Can anyone help me.谁能帮我。 I can also use moments js.我也可以使用 moments js。 You I need to use momentjs then please tell me.你我需要使用 momentjs 然后请告诉我。

There are many ways to accomplish that: This is one有很多方法可以做到这一点:这是一个

 const data = [ {date: "2022-02-22 18:00:00"}, {date: "2022-02-22 17:00:00"}, {date: "2022-02-22 18:00:00"}, {date: "2022-02-23 01:00:00"}, {date: "2022-02-23 10:00:00"}, {date: "2022-02-24 18:00:00"} ]; // simplified explanation: let reminder = []; let result = data.filter( item => { // 1st iteration: | 2nd iteration: | 3rd iteration: | 4th iteration: | 5th iteration: let date = item.date.split(' ')[0]; // 2022-02-22 | 2022-02-22 | 2022-02-22 | 2022-02-23 | 2022-02-24 if(reminder.includes(date)) { // does not include | reminder has this | reminder has this | does not include | does not include return false; // will not get here | go to next item | go to next item | will not get here | will not get here } reminder.push(date); // push to reminder | (will never reach | (will never reach | push to reminder | push to reminder return true; // add to result | those lines) | those lines) | add to result | add to result }); console.log('result', result)

Read more about the used functions: Array.filter() , String.split() , Array.includes() and Array.push()阅读有关所用函数的更多信息: Array.filter()String.split()Array.includes()Array.push()

Update, as discussed in the comments更新,如评论中所讨论

If you are dealing with a big amount of data you may better use new Set() with Set.has() and Set.add() see the next example using new Set()如果您正在处理大量数据,您最好将new Set()Set.has()Set.add()结合使用,请参阅下一个使用new Set()的示例

 const data = [ {date: "2022-02-22 18:00:00"}, {date: "2022-02-22 17:00:00"}, {date: "2022-02-22 18:00:00"}, {date: "2022-02-23 01:00:00"}, {date: "2022-02-23 10:00:00"}, {date: "2022-02-24 18:00:00"} ]; // simplified explanation: let reminder = new Set(); let result = data.filter( item => { // 1st iteration: | 2nd iteration: | 3rd iteration: | 4th iteration: | 5th iteration: let date = item.date.split(' ')[0]; // 2022-02-22 | 2022-02-22 | 2022-02-22 | 2022-02-23 | 2022-02-24 if(reminder.has(date)) { // does not have | reminder has this | reminder has this | does not have | does not have return false; // will not get here | go to 3rd | go to 4th | will not get here | will not get here } reminder.add(date); // push to reminder | (will never reach | (will never reach | push to reminder | push to reminder return true; // add to result | those lines) | those lines) | add to result | add to result // go to 2nd | | | go to 5th | finished }); console.log('result', result);

The following should be what you want.以下应该是你想要的。 Please let me know if this works for you.如果这对您有用,请告诉我。

const array = [
    {date: "2022-02-22 18:00:00"},
    {date: "2022-02-22 17:00:00"},
    {date: "2022-02-22 18:00:00"},
    {date: "2022-02-23 01:00:00"},
    {date: "2022-02-23 10:00:00"},
    {date: "2022-02-24 18:00:00"}
]

// Slice first 10 characters of array and check for duplicates. Keep first element found.
const unique = array.reduce((acc, curr) => {
    if (acc.length === 0) {
        acc.push(curr)
    } else {
        const found = acc.find(e => e.date.slice(0, 10) === curr.date.slice(0, 10))
        if (!found) {
            acc.push(curr)
        }
    }
    return acc
}, [])

console.log(unique)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM