我有一个数组
[
{"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,然后取消发布数组中的“unplanned”条目。
例如
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++;
}
});
});
但是我没有得到如何在循环中添加第二个条件来检查每个索引的releases数组中是否存在计划外条目。请帮我继续。
答案 0 :(得分:1)
使用逻辑运算符可以非常轻松地检查任何语句中的第二个条件。
在您的示例中,只需检查
即可if(release.needToBeDeliver == true && release.name == "unplanned"){
i++;
}
将允许您“过滤掉”需要传递的元素并将其命名为“unplanned”。希望,这就是你要找的。 p>
答案 1 :(得分:1)
您先获取计数,然后过滤releases
。
var array = [{ 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 }] }];
array.forEach(function (o) {
var count = o.releases.reduce((s, { needToBeDeliver }) => s + needToBeDeliver, 0);
o.releases = o.releases.filter(a => !(a.name === 'unplanned' && count));
});
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 2 :(得分:1)
要求“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')
});
答案 3 :(得分:0)
我要做以下事情:
var result = tickets.map(ticket => {
if (!ticket.releases.some(r => r.needToBeDeliver)) return ticket;
return Object.assign(ticket, { releases: ticket.releases.filter(r => r.name !== 'unplanned') });
});
如果错误,请检查是否有包含needToBeDeliver
的版本返回原始版本。如果为true,请过滤掉所有unplanned
版本。