All about my array:
var list = []
function arrObject(name, coins, id) {
this.name = name
this.coins = coins
this.id = id
this.listed = function(){
return (this.name + "" +"["+this.coins+"]")
}
}
list.push(new arrObject(user.username, coinsamount, user.id))
After adding two users my console.log output looks like:
[
{
name: 'Mango',
coins: '19',
id: '454214634158999514',
listed: [Function (anonymous)]
}
]
[
{
name: 'Goodmg',
coins: '41',
id: '721805465937021781',
listed: [Function (anonymous)]
}
]
How can I take "coins" values and sum them to one variable?
EDIT.1 changed function from Object
to arrObject
EDIT.2
const Discord = require('discord.js')
const embeds = require('./../../embeds.js')
var running = 0
module.exports = {
commands: 'rjoin',
minArgs: 1,
maxArgs: 2,
expectedArgs: '**coins amount**',
callback: (message, arguments) => {
function calcCoinSum(list) {
return list.reduce((prev, curr) => {
return prev + Number.parseInt(curr.coins);
}, 0);
}
const channelmessage = message.client.channels.cache.get('802537184824524820')
const coinsamount = arguments[0]
function wait(miliseconds){
return new Promise(resolve => setTimeout(resolve, miliseconds))
}
//channelmessage.send(value.name + `: **${value.coins}** coins`)
async function loop(){
while(true){
await wait(5000)
if(running === 2){
console.log(calcCoinSum(list))
break
}
}
}
function arrObject(name, coins, id) {
this.name = name
this.coins = coins
this.id = id
this.listed = function(){
return (this.name + "" +"["+this.coins+"]")
}
}
const list = []
if(message.channel.id === '802611173118705684'){
loop()
var user = message.author
if(running === 0){
console.log('Roll Started!')
channelmessage.send('Roluette has started! Type ?rjoin (coins amount) to join!')
list.push(new arrObject(user.username, coinsamount, user.id))
running = 1
setTimeout(() => {
running = 2
console.log('Roll ended')
}, 15000);
return
}
if(running === 1){
list.push(new arrObject(user.username, coinsamount, user.id))
}
}
}
}
question from:
https://stackoverflow.com/questions/65872624/how-to-read-specific-value-from-object-array-and-add-all-values-to-one-variable 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…