Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
83 views
in Technique[技术] by (71.8m points)

javascript - How to read specific value from object array and add all values to one variable?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using reduce this should be fairly simple:

function calcCoinSum(list) {
    return list.reduce((prev, curr) => {
        return prev + Number.parseInt(curr.coins);
    }, 0);
}

function arrObject(name, coins, id) {
    this.name = name
    this.coins = coins
    this.id = id
    this.listed = function () {
        return (this.name + "" + "[" + this.coins + "]")
    }
}

const list = [];
list.push(new arrObject('Mango', '19', '454214634158999514'));
list.push(new arrObject('Goodmg', '41', '721805465937021781'));

console.log('The coins sum is:', calcCoinSum(list))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...