gpt4 book ai didi

javascript - 如何将数组中的值的推送和更新结合起来?

转载 作者:行者123 更新时间:2023-12-02 23:53:28 26 4
gpt4 key购买 nike

如何组合数组的推送和更新事件?

我尝试使用分配,但不起作用

$(`#saveall${roomname}`).on('click',function() {
var tisch = document.getElementById(`${roomname}_tisch`).value;
item = {};
item ["tisch"] = tisch;
itemsnew.push(item);
});

如果我更新值我会遇到什么问题我得到了一个新条目:

    items: Array(2)
0: {tisch: "3"}
1: {tisch: "6"}

我仍然会更新 tisch 中的值,而不是数组中的新条目

最佳答案

{tisch: tisch} 在一个表达式中创建相同的对象;事实上,在 ES6 中您可以简单地使用简写 {tisch}。鉴于此:

itemsnew.push({tisch});

关于javascript - 如何将数组中的值的推送和更新结合起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55536792/

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