【问题标题】:How to modify object stored in local storage in javascript?如何在javascript中修改存储在本地存储中的对象?
【发布时间】:2021-11-16 00:06:43
【问题描述】:

我已将对象存储在本地存储中,想要更新它并再次存储它,我该怎么做?

这是对象

{
    "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, 
}

我想更新两个属性initial_kgfinal_kg 然后再次保存。

我如何从 lcoal 存储中获取对象

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

我如何在本地存储中设置对象

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

只有修改两个属性

【问题讨论】:

  • 得到对象后修改offline_work_order的属性,然后保存。

标签: javascript vue.js object


【解决方案1】:

如果你只想修改并放回去......

代码:

    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.

希望这是你想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 2011-08-21
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多