gpt4 book ai didi

javascript - 如何在空对象中添加新的键和值?

转载 作者:行者123 更新时间:2023-12-01 00:35:25 25 4
gpt4 key购买 nike

我有一个 null 对象。我想在其中添加动态键和对象。

尝试过

this.myObj[`${dayValue}`] = {};  //1

this.withDayTimeSlot[dayValue] = [targetValue];

错误

TypeError: Cannot set property '5' of null at ...

我的动态对象将如下所示。

{
'5':[],
'6':[]
}

最佳答案

您无法在 null 对象中设置属性。它需要首先作为空对象 {} 启动。

this.myObj = {};
this.myObj[`${dayValue}`] = []; // [] since your desired value in the object is an empty array [] and not an empty object {}

关于javascript - 如何在空对象中添加新的键和值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58166536/

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