如何使用 angular 获取数组中对象的索引?

编程入门 行业动态 更新时间:2024-10-27 01:24:34
本文介绍了如何使用 angular 获取数组中对象的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-23 00:00:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031292.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   组中   索引   对象   angular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!