我想检查数组中是否有一个项目具有相同的“数据”。如果是这种情况,我希望程序什么都不做,但如果不是这种情况,我希望它将它添加到我的数组中。
我已经有了以下代码。这只是从 html 中的输入字段获取数据,并将数据放入数组中。
我只需要检查里面是否已经有一个具有相同“数据”的项目,但我不知道如何完成这项工作。
store(newValue:number, newDatum, newAanwezig, newComment){
let billable = new BillableHours();
billable.datum = newDatum;
billable.hours = +newValue;
billable.aanwezig = newAanwezig;
billable.comment = newComment;
if(billable.aanwezig == "Aanwezig" && billable.hours !== null && billable.datum !== null) {
this.urenRegistratie.push(billable);
}
}
最佳答案
if (urenRegistratie.some(e => e.datum === newDatum)) {
/* contains the element we're looking for */
}
关于javascript - 在使用 Angular 将项目添加到数组之前,如何检查它是否已经在数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58955160/