[英]How can I add new key values to existing ones in JavaScript?
For example, I have a dictionary with a key such as 'run' and I want to continue adding different values to the run key such as 1,3,4 which will give me the key 'run' with the value 8.例如,我有一个带有诸如“run”之类的键的字典,我想继续向运行键添加不同的值,例如 1、3、4,这将为我提供值为 8 的键“运行”。
I'm also having trouble converting my values from a string format to an integer/float.我也无法将我的值从字符串格式转换为整数/浮点数。
This is my JS code:这是我的 JS 代码:
tweet_array.forEach(element => {
var x = element.activityType
if (dict[(x[0])] in Object.keys(dict))
{
console.log('working')
dict[(x[0])] += x[1]
}
else
{
var s = dict[(x[0])]
dict[(x[0])] = x[1] + s
act.set(x[0],x[1])
}
}
//activityType returns an array with two elements such as ["run", "3.026"] //activityType返回一个有两个元素如["run", "3.026"]的数组
//tweet_array is an array with strings and activity types looks into the array and finds the type of activity such as run, walk, etc and the miles for it. //tweet_array 是一个包含字符串和活动类型的数组,查看数组并找到活动的类型,例如跑步、步行等以及它的里程。 I have trouble adding these values together and getting a dictionary with values such as run:12323, walk: 9888
我无法将这些值加在一起并获取包含诸如 run:12323, walk:9888 之类的值的字典
Create a map/object like so,像这样创建地图/对象,
const obj = {};
tweet_array.forEach((element) => {
const [activityName, activityValue] = element.activityType;
if (!obj.hasOwnProperty(activityName)) {
obj[activityName] = 0;
}
obj[activityName] += activityValue;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.