过滤并根据条件更新对象数组

huangapple go评论61阅读模式
英文:

Filter and update array of objects on condition

问题

我正在从MongoDB获取文档并将其存储在books变量中。
books是一个对象数组,我试图对其进行迭代,但为了这个示例,我只保留了books中的一个对象。

这里我想比较users数组中每个对象的dueDate,并只保留那些dueDate小于当前日期的对象,并在该对象内添加一个额外的fine键值对。

以下是我为此编写的函数:

books.forEach(function (arrayItem) {
    arrayItem.users = arrayItem.users.filter(
      (user) => user.dueDate < new Date().toISOString()
    );
    arrayItem.users.forEach((user) => {
      user.fine = (new Date(user.dueDate) - new Date()) / (1000 * 60 * 60 * 24) * 100; // 计算罚款
    });
});

通过这样做,我能够筛选出小于当前日期的对象,并为对象添加fine属性,fine的计算方式是(dueDate - 当前日期)* 100,其中一天的延迟罚款为100卢比。

现在,books看起来应该像这样:

books=[{
        "users": [
            {
                "id": "63abd9ed787e941505b3a816",
                "dueDate": "2023-01-06T07:28:42.461Z",
                "fine": 100
            }
        ],
}]

希望这对你有所帮助。如有任何问题,请随时提出。

英文:

I am getting documents from mongodb and storing it in books variable.
books is an array of objects on which i am trying to iterate but for this example i have kept only one object in books

const books = [{
        &quot;users&quot;: [
            {
                &quot;id&quot;: &quot;1&quot;,
                &quot;dueDate&quot;: &quot;2023-01-06T07:28:42.461Z&quot;
            },
            {
                &quot;id&quot;: &quot;2&quot;,
                &quot;dueDate&quot;: &quot;2023-01-08T07:34:46.095Z&quot;
            }
        ],
}]

Here i want to compare dueDate for each object in users array and keep only that object which has dueDate less than current date along with that add an additional key of fine inside that object.

here is the function i wrote for it

books.forEach(function (arrayItem) {
    arrayItem.users = arrayItem.users.filter(
      (user) =&gt; user.dueDate &lt; new Date().toISOString()
    );
  });

by this i am able to filter objects less than cuurent date but i cannot add addition key value pair to the object

My answer look like

books=[{
        &quot;users&quot;: [
            {
                &quot;id&quot;: &quot;63abd9ed787e941505b3a816&quot;,
                &quot;dueDate&quot;: &quot;2023-01-06T07:28:42.461Z&quot;
            }
        ],
}]

but i want it like this where one day delay has 100 rupees as fine
fine = (dueDate-current date)*100

books=[{
        &quot;users&quot;: [
            {
                &quot;id&quot;: &quot;63abd9ed787e941505b3a816&quot;,
                &quot;dueDate&quot;: &quot;2023-01-06T07:28:42.461Z&quot;,
                &quot;fine&quot;:100
            }
        ],
}]

i tried using few things but did not workout for me. Any help is very much appreciated. Thanks in advance.

答案1

得分: 2

你可以在filter()之后添加一个map(),以便为每个对象添加一个新字段。

const books = [{
  users: [
    {
      id: "1",
      dueDate: "2023-01-06T07:28:42.461Z"
    },
    {
      id: "2",
      dueDate: "2023-01-08T07:34:46.095Z"
    },
  ],
}]

books.forEach((arrayItem) => {
    arrayItem.users = arrayItem.users
    .filter((user) => user.dueDate < new Date().toISOString())
    .map((user) => ({ ...user, fine: (new Date() - new Date(user.dueDate)) * 100 }));
  });

console.log(books)
英文:

You can add a map() after filter()in order to add a new field to each object

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const books = [{
  users: [
    {
      id: &quot;1&quot;,
      dueDate: &quot;2023-01-06T07:28:42.461Z&quot;
    },
    {
      id: &quot;2&quot;,
      dueDate: &quot;2023-01-08T07:34:46.095Z&quot;
    },
  ],
}]

books.forEach((arrayItem) =&gt; {
    arrayItem.users = arrayItem.users
    .filter((user) =&gt; user.dueDate &lt; new Date().toISOString())
    .map((user) =&gt; ({ ...user, fine: (new Date() - new Date(user.dueDate)) * 100 }));
  });
  
console.log(books)

<!-- end snippet -->

答案2

得分: 0

use ```.map```

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

let books=[{
    "users": [
        {
            "id": "63abd9ed787e941505b3a816",
            "dueDate": "2023-01-06T07:28:42.461Z"
        }
    ],
}]

let currDate = new Date().toISOString();
let res =books.map( b =&gt; {
    return b.users = b.users.map( u =&gt; {
        return (u.dueDate &lt; currDate) ? {...u, fine: 100} : {...u}
    } 
    );
});

console.log(res)

&lt;!-- end snippet --&gt;
英文:

use .map

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let books=[{
        &quot;users&quot;: [
            {
                &quot;id&quot;: &quot;63abd9ed787e941505b3a816&quot;,
                &quot;dueDate&quot;: &quot;2023-01-06T07:28:42.461Z&quot;
            }
        ],
}]

let currDate = new Date().toISOString();
let res =books.map( b =&gt; {
    return b.users = b.users.map( u =&gt; {
      return (u.dueDate &lt; currDate) ? {...u, fine: 100} : {...u}
    } 
    );
  });

console.log(res)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月7日 17:22:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75039249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定