30

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

    }

  }
0

6 Answers 6

51

As mdn says:

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.

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

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

Sign up to request clarification or add additional context in comments.

Comments

5

Adding to the answer of @StepUp (and please select his answer as best, as it replies the question greatly):

Finding the index is not related to Angular, it is rather Vanilla JavaScript.

In your case, you need a function that has as parameter the newDatum "needle" to be searched in the array "haystack" using findIndex:

var getNewDatumIndex = (array, newDatumNeedle) => {
  return array.findIndex(x => x.newDatum === newDatumNeedle); 
}

You can also add the method in the array prototype if you want, in order to ommit the first argument:

Array.prototype.getNewDatumIndex = (newDatumNeedle) => {
  return this.findIndex(x => x.newDatum === newDatumNeedle); 
}

Comments

3

An easy and elegant solution is to use _.isEqual, which performs a deep comparison using lodash library

Npm Package -- https://www.npmjs.com/package/lodash

const a = [
   { firstName: "Adam", LastName: "Howard" },
   { firstName: "Ben", LastName: "Skeet" },
   { firstName: "Joseph", LastName: "Skeet" }
];
const find =  { firstName: "Joseph", LastName: "Skeet" }

index = a.findIndex(x => _.isEqual(x, find));

console.log(index);

Comments

1

store(newValue: number, newDatum, newAanwezig, newComment) { let index = this.urenRegistratie.indexOf(existingDatum);

const existingDatum = this.urenRegistratie.find(billable => {
  return billable.datum === newDatum;
  return
});

if (index != -1) {
  let index = this.urenRegistratie.indexOf(existingDatum);
  existingDatum.splice(index, 1)
} else {

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

}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0
let arr = [6, 4, 9, 5, 56, 7];
let selectedNumber = 9;

// Find the index of the selected number
let index = arr.indexOf(selectedNumber);

if (index !== -1) {
// Get previous and next values
let previousValue = index > 0 ? 
arr[index - 1] : undefined; // If it 
exists
let nextValue = index < arr.length - 1 ? 
arr[index + 1] : undefined; // If it 
exists

console.log(`Previous: ${previousValue}, 
Next: ${nextValue}`);
} else {
    console.log("Selected number not 
found.");
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

sometimes, if you compare by id, you need to parseint, cause object's id is a string. care of this

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.