Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (19.9k points)

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);

But it keeps returning -1 and I don't know why this is happening.

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;

        billable.comment = newComment;

        if(billable.aanwezig == "Aanwezig" && billable.hours !== 0 && billable.datum !== null) {

          this.urenRegistratie.push(billable);

        }

    }

    if(existingDatum) {

    }

  }

1 Answer

0 votes
by (25.1k points)
edited by

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" }];

index = a.findIndex(x => x.LastName === "Skeet");

console.log(index);

Check out this Angularjs online Course to enhance your career! 

Browse Categories

...