问题描述
限时送ChatGPT账号..我需要在数组中有一个对象的索引,以便我可以删除数组的这一部分.我试过用这个:
I need to have the index of an object in array so I can delete this part of the array. I tried using this:
var index = this.urenRegistratie.indexOf(newDatum);
但它一直返回-1,我不知道为什么会这样.
But it keeps returning -1 and I don't know why this is happening.
这是我拥有的代码的一部分.它从 html 的表单中获取数据并将其放入我的数组中,现在我已经准备好了 if 语句( exisitingDatum ),我的代码需要在那里.有人可以帮我一下吗?
this is the part of the code I have. it gets data out of a form in html and places that into my array, now I already have an if statement ready ( exisitingDatum ) , my code needs to be in there. Can someone please help me out a bit?
store(newValue:number, newDatum, newAanwezig, newComment){
const existingDatum = this.urenRegistratie.find(billable => {
return billable.datum === newDatum;
return
});
if (!existingDatum) {
let billable = new BillableHours();
billable.datum = newDatum;
billable.hours = +newValue;
billable.aanwezig = newAanwezig;
billablement = newComment;
if(billable.aanwezig == "Aanwezig" && billable.hours !== 0 && billable.datum !== null) {
this.urenRegistratie.push(billable);
}
}
if(existingDatum) {
}
}
推荐答案
正如 mdn 所说:
findIndex() 方法返回第一个元素的索引满足提供的测试函数的数组.否则,它返回-1,表示没有元素通过测试.
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
如果你有这样的对象数组,那么尝试使用findIndex
:
If you have array of objects like this, then try to use findIndex
:
const a = [
{ firstName: "Adam", LastName: "Howard" },
{ firstName: "Ben", LastName: "Skeet" },
{ firstName: "Joseph", LastName: "Skeet" }
];
let index = a.findIndex(x => x.LastName === "Skeet");
console.log(index);
这篇关于如何使用 angular 获取数组中对象的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论