gpt4 book ai didi

javascript - 如何检查数组中是否存在值并且值的计数大于 1?

转载 作者:行者123 更新时间:2023-11-30 11:25:48 25 4
gpt4 key购买 nike

我有一个数组

[
{"name":"Ticket1","releases":[{"needToBeDeliver":true,"name":"release1","delivered":false},{"needToBeDeliver":true,"name":"release2","delivered":false}]},
{"name":"Ticket2","releases":[{"needToBeDeliver":true,"name":"release1","delivered":false},{"needToBeDeliver":true,"name":"release2","delivered":false},{"needToBeDeliver":false,"name":"unplanned","delivered":false}]},
{"name":"Ticket3","releases":[{"needToBeDeliver":false,"name":"release1","delivered":false},{"needToBeDeliver":false,"name":"unplanned","delivered":false}]},
{"name":"Ticket4","releases":[{"needToBeDeliver":false,"name":"unplanned","delivered":false}]}
]

在上面的数组中,我必须检查 releases 数组是否包含“unplanned”条目和 count(releases.needToBeDeliver == true) > 0,然后从 releases 数组中取消设置“unplanned”条目。

例如

  • 在第一个索引中,它将保持原样,因为它不包含 releases 数组中的任何计划外条目
  • 在第二个索引中,它包含计划外条目并且 needToBeDeliver 值多次为真,删除计划外条目
  • 在第三个索引中,它包含计划外的条目但needToBeDeliver 不等于true,不要删除计划外的条目
  • 在第四个索引中,它包含计划外条目但 needToBeDeliver 不为真,不要删除计划外条目

O/p 应该在后面

[
{"name":"Ticket1","releases":[{"needToBeDeliver":true,"name":"release1","delivered":false},{"needToBeDeliver":true,"name":"release2","delivered":false}]},
{"name":"Ticket2","releases":[{"needToBeDeliver":true,"name":"release1","delivered":false},{"needToBeDeliver":true,"name":"release2","delivered":false}]},
{"name":"Ticket3","releases":[{"needToBeDeliver":false,"name":"release1","delivered":false},{"needToBeDeliver":false,"name":"unplanned","delivered":false}]},
{"name":"Ticket4","releases":[{"needToBeDeliver":false,"name":"unplanned","delivered":false}]}
]

到目前为止我尝试了什么:

tickets.forEach(ticketsData => {
var i = 0;
ticketsData.releases.forEach(release => {
if(release.needToBeDeliver === true){
i++;
}
});
});

但我不知道如何在循环中添加第二个条件来检查每个索引的发布数组中是否存在计划外条目。请帮助我继续进行。

最佳答案

要求“count(releases.needToBeDeliver == true) > 0”可以改写为“有一个带有 .needToBeDeliver == true 的版本”,您可以使用 .some:

data.forEach(d => {
if (d.releases.some(r => r.needToBeDeliver))
d.releases = d.releases.filter(r => r.name !== 'unplanned')
});

关于javascript - 如何检查数组中是否存在值并且值的计数大于 1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48077199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com