gpt4 book ai didi

javascript - 将数组结果从一个 json 对象移动到另一个

转载 作者:行者123 更新时间:2023-11-30 11:08:45 25 4
gpt4 key购买 nike

我在 _initializeData 中有一个新创建的 json 数据模型。目的是当用户尝试删除购物车中的商品时,我想将该商品存储在这个新数据模型中。因此,在单击删除按钮时,我正在读取包含购物车中所有项目的 json 数据模型,然后使用已删除元素的索引将结果复制到新模型中。如果我只删除一个项目,它工作正常。但是如果我在另一个项目上按删除,新模型中的先前条目将被新条目覆盖。它不像数组那样包含所有条目。

_initializeData: function () {
var removedData = {}; // new model for removed parts
removedData.removedParts = [];
this.removedItems = new JSONModel(removedData);
this.getView().setModel(this.removedItems, "removedItems");
}

// button click
onRemovePart: function (oEvent) {
var idx = this.getView().byId("table").getSelectedIndex();

// retrieveing the model which has all the parts from the table
var data = this.getView().getModel("materialData").getProperty("/partsData");

// reading the removed item data
var removedPart = data[idx];

// assigning it to the new model. But it does not hold multiple
entries rather over-writes the previous one
this.getView().getModel("removedItems").setProperty("/removedParts", removedPart);

最佳答案

使用 Array.slice() 来避免引用。在这里,引用导致了覆盖行为

关于javascript - 将数组结果从一个 json 对象移动到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54623254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com