我想检查数组中是否有带有相同“基准”的项。如果是这样,我希望程序什么也不做,但是如果不是,我希望它将其添加到我的数组中。
我已经有以下代码。这只是从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);
}
}
// Find in your array if there is an item with that datum already.
const existingDatum = this.urenRegistratie.find(billable => {
return billable.datum === newDatum;
});
// If you don't have an existing item with the same datum, then you can create and add it to the array
if (!existingDatum) {
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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句