如何在VUE或JavaScript中通过此上下文中的文字修改对象值?

发布于 2025-01-27 10:48:13 字数 1821 浏览 2 评论 0原文

我在vue.js中,并试图纠正不适当的数量输入值,例如 “ 12JD” 。它们属于 item 对象,该对象在数组文字中,称为 store (在data()返回中定义)。有问题的代码在输入事件处理程序功能中,该功能获得 item 的索引 - idx ,其中写入不良数量。

一切顺利,直到我尝试将适当的数组(没有坏数量)分配给上下文中的原始商店。该部分不起作用,原始部分没有任何值更改。

failed(idx) {

  const storeCopy = lodash.cloneDeep(this.store); //deep copy the store literal

  const unmodified = storeCopy.filter((item) => item.id !== idx);

  const modified = storeCopy.filter((item) => item.id === idx)[0]; //slice out the relevant part
  const modifiedCopy = JSON.parse(JSON.stringify(modified)); //deep copy the item object

  const input = parseInt((" " + modifiedCopy.quantity).slice(1)); //get parsed input from deep copied string

  const store = [
    Object.defineProperty(modifiedCopy, "quantity", {
      value: isNaN(input) ? 0 : input,
    }),
    ...unmodified,
  ]; //correct the input and put it back to the array
  console.log(store);

  this.store.length = 0; //from this it's messed up
  this.store = store;
}

我进行了深层复制三遍,但问题仍然倒置到插入最后两行后的控制阵列。没有他们,这很好。

为什么参考将不收集垃圾,在设置 this.store.length 或 this.store 完全呢? 我会错过什么,也许在新数组中剩下任何原始参考?任何帮助!谢谢

编辑

这是输出的,没有两行的输出:

“没有最后两行”

我什至不明白本地范围中的商店在本地范围中可能会通过插入来影响谁那两行。无论如何,它将以某种方式恢复到不良值“ 12JD”。

”两行“

也看起来有所不同,我也必须单击三个点才能显示值。

I am in Vue.js and trying to correct inappropriate quantity input value like "12jd". They belong to item objects which are in a array literal called store (which is defined in data( ) return). The problematic code is in the input event handler function which gets the item's index - idx in which the bad quantity was written.

Everything goes well until I try to assign the proper array, with no bad quantity, to the original store in the this context. This part does not work, no value gets changed in the original.

failed(idx) {

  const storeCopy = lodash.cloneDeep(this.store); //deep copy the store literal

  const unmodified = storeCopy.filter((item) => item.id !== idx);

  const modified = storeCopy.filter((item) => item.id === idx)[0]; //slice out the relevant part
  const modifiedCopy = JSON.parse(JSON.stringify(modified)); //deep copy the item object

  const input = parseInt((" " + modifiedCopy.quantity).slice(1)); //get parsed input from deep copied string

  const store = [
    Object.defineProperty(modifiedCopy, "quantity", {
      value: isNaN(input) ? 0 : input,
    }),
    ...unmodified,
  ]; //correct the input and put it back to the array
  console.log(store);

  this.store.length = 0; //from this it's messed up
  this.store = store;
}

I do deep copy even three times, still the problem backpropagates to console logged array after uncommenting the last two lines. Without them it's fine.

Why does the reference not get garbage collected, after setting this.store.length to 0, or this.store to [] at all?
What do I miss out, is there any original reference left in the new array maybe? Any help appreciated! Thanks

Edited

Here's the output without then with the two last line:

Without the last two lines

I don't even understand who it's possible that store in the local scope is affected backwards, by inserting those two lines. Anyways It gets restored somehow to the bad value "12jd".

With the two lines

Even the log looks different, I have to click on the three dots to reveal the values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文