Q:

validate duplicated data before add to local storage in ionic framework 3+

hope you are doing well, today i need your help to solve a little problem, i have a app where i get data from a bluetooth device and store it in local storage (for now), im using the timestamp of the device data as primary key, so, i want to validate this primary key with the items who already are stored in the localstorage before add a new one to evade add duplicated data, because now the app only get the data from the device and store it without validate if the value already exist.
this is my current code:

// INTERFACE

glucometerData: GlucoseMeasurement = {
   id: ''
    glucoseValue: '',
   date: ''
};
// THE FUNCTION IN MY PAGE.TS
saveData(glucometerData) {
   this.functionUtil.addItem('glucometer', glucometerData).then(item => {
     this.loadItems();
     this.ui.showToast('Saved ' + item.length + ' items.');
    });

 }
 
 // SERVICE FUNCTION ADDITEM
  addItem(ITEMS_KEY, item: any): Promise<any> {
    return this.storage.get(ITEMS_KEY).then((items: any[]) => {
      if (items) {
        items.push(item);
        return this.storage.set(ITEMS_KEY, items);
      } else {
        return this.storage.set(ITEMS_KEY, [item]);
      }
    });
  }

any help is welcome.
thanks in advance

A:

// SERVICE FUNCTION ADDITEM
addItem(ITEMS_KEY, item: any): Promise<any> {
    return this.storage.get(ITEMS_KEY).then((items: any[]) => {
        const newItems: GlucoseMeasurement[] = [];
        if (items) {
            for (const i of items) {
                if (i.date === item.date) {
                    // manage
                    return 'Ya existe';
                } else {
                    newItems.push(i);
                }
            }
            return this.storage.set(ITEMS_KEY, newItems);
        } else {
            return this.storage.set(ITEMS_KEY, [item]);
       }
  });
}

// Validate if item in storage exists with the same date value

