如何从对象数组中读取特定值并将所有值添加到一个变量中?
Posted
技术标签:
【中文标题】如何从对象数组中读取特定值并将所有值添加到一个变量中?【英文标题】:How to read specific value from object array and add all values to one variable? 【发布时间】:2021-04-28 12:59:50 【问题描述】:关于我的数组:
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))
添加两个用户后,我的 console.log 输出如下所示:
[
name: 'Mango',
coins: '19',
id: '454214634158999514',
listed: [Function (anonymous)]
]
[
name: 'Goodmg',
coins: '41',
id: '721805465937021781',
listed: [Function (anonymous)]
]
如何获取“硬币”值并将它们加到一个变量中?
EDIT.1 将函数从 Object
更改为 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))
【问题讨论】:
Object
是表示数据类型的JS类,换个名字
我认为您可以通过在循环中使用arr[i][0].coins
来获得coins
@Pentium1080Ti 我试过了,当我在控制台登录时,它的值出现在 2 行而不是 1 行中
【参考方案1】:
使用reduce 这应该相当简单:
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))
【讨论】:
控制台日志中的新输出为:26 26
不,上面的代码肯定会输出The coins sum is: 60
- 你甚至可以自己尝试,只需点击“运行代码sn-p” ...
我添加了整个代码,也许数组之外有问题?【参考方案2】:
您可以做的是遍历列表,然后访问列表的每个元素。列表中的每个成员都是一个对象,其键中有一个值 coins
。您可以访问该值并将其汇总并将其存储在变量中。
function calculateCoins (list)
int sum = 0
for(let i of list)
sum += parseInt(i.coins)
return sum
【讨论】:
它还会输出两个相邻的值。可能是代码有问题? 你能详细说明吗?list
变量实际上只是一个像这样的对象数组:[ name: 'Mango', coins: '19', id: '454214634158999514', listed: [Function (anonymous)] ] [ name: 'Goodmg', coins: '41', id: '721805465937021781', listed: [Function (anonymous)] ]
现在如果你将它传递给我给出的函数,你将得到硬币的总和。以上是关于如何从对象数组中读取特定值并将所有值添加到一个变量中?的主要内容,如果未能解决你的问题,请参考以下文章