英文:
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 = [{
"users": [
{
"id": "1",
"dueDate": "2023-01-06T07:28:42.461Z"
},
{
"id": "2",
"dueDate": "2023-01-08T07:34:46.095Z"
}
],
}]
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) => user.dueDate < 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=[{
"users": [
{
"id": "63abd9ed787e941505b3a816",
"dueDate": "2023-01-06T07:28:42.461Z"
}
],
}]
but i want it like this where one day delay has 100 rupees as fine
fine = (dueDate-current date)*100
books=[{
"users": [
{
"id": "63abd9ed787e941505b3a816",
"dueDate": "2023-01-06T07:28:42.461Z",
"fine":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: "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)
<!-- end snippet -->
答案2
得分: 0
use ```.map```
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let books=[{
"users": [
{
"id": "63abd9ed787e941505b3a816",
"dueDate": "2023-01-06T07:28:42.461Z"
}
],
}]
let currDate = new Date().toISOString();
let res =books.map( b => {
return b.users = b.users.map( u => {
return (u.dueDate < currDate) ? {...u, fine: 100} : {...u}
}
);
});
console.log(res)
<!-- end snippet -->
英文:
use .map
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let books=[{
"users": [
{
"id": "63abd9ed787e941505b3a816",
"dueDate": "2023-01-06T07:28:42.461Z"
}
],
}]
let currDate = new Date().toISOString();
let res =books.map( b => {
return b.users = b.users.map( u => {
return (u.dueDate < currDate) ? {...u, fine: 100} : {...u}
}
);
});
console.log(res)
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论