1

I want to check if there is an item inside of the array with the same 'datum'. If that is the case I want the program to do nothing, but if that's not the case I want it to add it to my array.

I already have the following code. this just gets the data from input fields in the html, and places the data inside the array.

I only need to check if there's already an item inside with the same 'datum', but I have no idea how to get this done.

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

  }
1
  • 1
    What about tu use Set: let set = new Set(); on urenRegistratie object? Commented Nov 20, 2019 at 13:14

6 Answers 6

2
if (urenRegistratie.some(e => e.datum === newDatum)) {
/* contains the element we're looking for */
}
Sign up to request clarification or add additional context in comments.

Comments

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

Read more about JS Array Find here

1 Comment

developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… In the future: Say you wanted to modify or update the existing object that already has datum with the new data. You could use map to return a new array with the modified item inside the callback
1

you can use 'find' or 'findIndex' as follow:

with 'findIndex':

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.findIndex(f=>f.datum ==billable.datum)>-1) {
      this.urenRegistratie.push(billable);
    }

  }

or 'find'

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.find(f=>f.datum ==billable.datum)) {
      this.urenRegistratie.push(billable);
    }

  }

Good Luck!!!!

Comments

0

Try using:

let billable = new BillableHours();
    billable.datum = newDatum;
    billable.hours = +newValue;
    billable.aanwezig = newAanwezig;
    billable.comment = newComment;
var flag = false;
this.urenRegistratie.forEach(elem => {
    if(elem.datum === newDatum){
      flag = true;
    }
});
if(billable.aanwezig == "Aanwezig" && billable.hours !== null && billable.datum !== null) {
if(!flag){
this.urenRegistratie.push(billable);
}
}

Comments

0

You can use some() to do it.

Try like this:

Working Demo

let billable = { aanwezig: "Aanwezig", hours: 12 }
if (!this.urenRegistratie.some(x => x.aanwezig == billable.aanwezig && x.hours == billable.hours)) {
  this.urenRegistratie.push(billable);
}

1 Comment

Since it's Angular (typescript), it's a good practice to do strict check: index === -1.
0

You can use the Array.prototype.find() function to see if there is already an entry with the same datum value. If not found, it will return undefined and if found you get the array element that matched. The code would look like this:

let element = this.urenRegistratie.find(e => e.datum === newDatum);
if(element) {
 // Found, update it 
}
else {
 // Not found insert new 
 this.urenRegistratie.push(new {})
}

See Array.prototype.find documentation for full details.

Comments

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.