Home > database >  How to modify object stored in local storage in javascript?
How to modify object stored in local storage in javascript?

Time:11-16

I have stored object in local storage want to update it and store it again how I can do it?

Here is the object

{
    "id": 123,
    "deal_id": 88,
    "internal_control_number": "345678",
    "export": 1,
    "exportation": 0,
    "organic": 0,
    "national": 1,
    "date_of_work": "2021-11-16",
    "agent": "26911",
    "ranch": null,
    "weighing_machine": "34",
    "type_of_cut": "23",
    "type_of_damage": "19",
    "boss_amount": 0,
    "cutting_company_amount": 0,
    "cutting_company_amount_type": null,
    "packaging_company_amount": 0,
    "packaging_company_amount_type": null,
    "number_of_boxes": "27",
    "size_of_boxes": "29",
    "fruit_delivery_location": "Empaque",
    "company_or_contractor": 0,
    "meeting_type": "ranch",
    "delivery_latitude": null,
    "delivery_longitude": null,
    "cutting_company": "21308",
    "packaging_company": "21128",
    "independent_contractor": null,
    "cutting_amount": null,
    "amount_of_kg": -555,
    "final_kg": 0,
initial_kg: 555, 
}

I want to update two properties initial_kg and final_kg and then save it again .

how I get the object from lcoal storage

    let offline_work_order = await JSON.parse( this.$localStorage.get(`work_order_${this.id}`));

How i set the the object in local storage

            this.$localStorage.set( `work_order_${this.id}`,JSON.stringify(this.work_order));

Only thing is modificationof two properties

CodePudding user response:

If all you want is to modify and put back...

Code:

    let data = JSON.parse(this.$localStorage.get(`work_order_${this.id}`));

    data.initial_kg = "Your value here";
    data.final_kg = "Your value here";

    this.$localStorage.set(`work_order_${this.id}`, JSON.stringify(data));

    // But maybe you could (Vanilla)

    let data = JSON.parse(localStorage.getItem(`work_order_${this.id}`));

    data.initial_kg = "Your value here";
    data.final_kg = "Your value here";

    localStorage.setItem(`work_order_${this.id}`, JSON.stringify(data));

    // Also, JSON parse and stringify aren't async methods, use them without await.

Hope this is what you wanted.

  • Related