在Javascript中添加包含特定字符串的字符串值

时间:2017-06-05 13:24:30

标签: javascript json node.js

我有一个名为guild的对象,它有以下数组:

members: [
{
uuid: "6afc724d566746c796d13b233c9960ea",
rank: "MEMBER",
joined: 1492733608170,
dailyCoins-1-5-2017: 920,
dailyCoins-2-5-2017: 790,
dailyCoins-3-5-2017: 260,
dailyCoins-4-5-2017: 790
},
{
uuid: "fa014557f1ac4325b23940fb872031a4",
rank: "MEMBER",
joined: 1493332956351
},
etc.

如何将包含字符串" dailyCoins"的每个字符串的值加在一起? ?

这是我目前的尝试:

 var guildMembers = [];
 var memberCoins = 0;
    for (i = 0; i < guild.members.length; i++) {
        for (j = 0; j < Object.keys(guild.members[i]).length; j++)
            console.log(Object.keys(guild.members[i]));
            if (Object.keys(guild.members[i])[j].includes("dailyCoins")) {
                var memberCoins = memberCoins + Object.values(guild.members[i])[j];
            }
        var member = {
            uuid: guild.members[i].uuid,
            rank: guild.members[i].rank,
            joined: guild.members[i].joined,
            coins: memberCoins
        };
        guildMembers.push(member);
    }

4 个答案:

答案 0 :(得分:1)

此代码应以您的方式解决。

int

答案 1 :(得分:0)

为什么要在循环中重新初始化memberCoins?

只需更改此行

即可
var memberCoins = memberCoins + Object.values(guild.members[i])[j];

memberCoins += Object.values(guild.members[i])[j];

完整代码

var guildMembers = [];
    var memberCoins = 0;
for (i = 0; i < guild.members.length; i++) {
    for (j = 0; j < Object.keys(guild.members[i]).length; j++) {
        console.log(Object.keys(guild.members[i]));

        if (Object.keys(guild.members[i])[j].includes("dailyCoins")) {
            memberCoins += Object.values(guild.members[i])[j];
        }
    }
var member = {
    uuid: guild.members[i].uuid,
        rank: guild.members[i].rank,
    joined: guild.members[i].joined,
    coins: memberCoins
};
guildMembers.push(member);
}

答案 2 :(得分:0)

var members=guild.members.map(function(el){
  return {
        uuid: el.uuid,
        rank: el.rank,
        joined: el.joined,
        coins: Object.keys(el).filter(str=>str.includes("dailyCoins")).reduce((c,str)=>c+el[str],0)
    };      
});

您可以使用Array.filter / reduce ...

来简化它

答案 3 :(得分:0)

您可以使用map来迭代您的集合,Object.keys(obj)来获取对象属性,然后使用filter来过滤所需的对象属性,使用reduce来累积结果:

&#13;
&#13;
var members  = [
  {
    'uuid': "6afc724d566746c796d13b233c9960ea",
    'rank': "MEMBER",
    'joined': 1492733608170,
    'dailyCoins-1-5-2017': 920,
    'dailyCoins-2-5-2017': 790,
    'dailyCoins-3-5-2017': 260,
    'dailyCoins-4-5-2017': 790
  },
  {
    'uuid': "fa014557f1ac4325b23940fb872031a4",
    'rank': "MEMBER",
    'joined': 1493332956351
  }
];

var guildMembers = members.map(function(row) { 
    var filter = function(key) { return key.indexOf('dailyCoins') >= 0; };        
    var reduce = function(sum, current) { return sum + row[current]; };
    
    return {
    	uuid: row.uuid,
    	rank: row.rank,
    	joined: row.joined,
    	coins: Object.keys(row).filter(filter).reduce(reduce, 0)
    };    
});

console.log(guildMembers);
&#13;
&#13;
&#13;