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.
I'm also having trouble converting my values from a string format to an integer/float.
This is my JS code:
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"]
//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. I have trouble adding these values together and getting a dictionary with values such as 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;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.