-1

i have an array that is been already loaded on to view, i need to update a specific object in array

modified_data(){
        this.nativeStorage.getItem("modifiedData").then((data)=>{
            console.log("modified_data fun", data);
        //  console.log(this.dailyDays);
            var array = this.dailyDays;
            for (var i = 0; i < array.length; i++) {
                var element = array[i];
            //  console.log("in aloop",element.day);
                if (element.day === data.day) {
                    console.log("got same da", element);
                    this.dailyDays.push({
                                    day: data.day,
                                    month: this.currentDate.getMonth(),
                                    year: this.currentDate.getFullYear(),
                                    price: data.price,
                                    brand: data.selectedBrand,
                                    howManyDay: data.selectedDay,
                                    quantity: data.selectedQuantity
                                });

                } else {

                }

            }
        })
}

By using the above code a new object gets added up at the bottom of the array in html,

The array in the view have a date listed if i find the same date then that date should be updated with the new object some one help me

5
  • Check if this post helps: stackoverflow.com/a/44121993/5556177 Commented Jul 10, 2017 at 12:43
  • Please follow below link stackoverflow.com/questions/4689856/… Commented Jul 10, 2017 at 12:51
  • could you give direct solution i am confused @Nehal Commented Jul 10, 2017 at 12:53
  • Need little clarification first, so, you are trying compare data object with all objects in array and if you don't find a match, you add data to array, if you do find a match, you need to replace the existing one with data, correct? Commented Jul 10, 2017 at 12:57
  • ya exactly but i need only to update the array with new data Commented Jul 10, 2017 at 13:00

1 Answer 1

0

From what I understand, the logic should look something like this. It looks for an existing object in the array, if it finds a match, returns the index, if not, returns -1. Based on that, you can perform

modified_data(){
  this.nativeStorage.getItem("modifiedData").then((data)=>{
    console.log("modified_data fun", data);
    //  console.log(this.dailyDays);
    // var array = this.dailyDays;
    let updateItem = this.dailyDays.find(this.findIndexToUpdate, data.day); 

    let index = this.dailyDays.indexOf(updateItem); 

    if(index > -1){
      // Add whatever logic you need to update existing object
      // below line will replace the whole object
      this.dailyDays[index] = data;   
    }
    else{
      this.dailyDays.push({
         day: data.day,
         month: this.currentDate.getMonth(),
         year: this.currentDate.getFullYear(),
         price: data.price,
         brand: data.selectedBrand,
         howManyDay: data.selectedDay,
         quantity: data.selectedQuantity
      })
    }   
  })

findIndexToUpdate(data) { 
        return data.day === this;
}
Sign up to request clarification or add additional context in comments.

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.