如何检查数组中是否存在值以及值的计数是否大于1?

时间:2018-01-03 12:14:49

标签: javascript foreach

我有一个数组

[
    {"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”条目。

例如

  • 在第一个索引中,它将保持原样,因为它不包含版本数组中的任何未计划条目
  • 在第二个索引中,它包含未计划的条目,且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++;
        }       
    });
});

但是我没有得到如何在循环中添加第二个条件来检查每个索引的releases数组中是否存在计划外条目。请帮我继续。

4 个答案:

答案 0 :(得分:1)

使用逻辑运算符可以非常轻松地检查任何语句中的第二个条件。

在您的示例中,只需检查

即可
if(release.needToBeDeliver == true && release.name == "unplanned"){
            i++;
        }

将允许您“过滤掉”需要传递的元素并将其命名为“unplanned”。希望,这就是你要找的。

答案 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版本。