如何根据名称增加数组 json 文件中的值?
How to increment values in an array json file based of from a name?
示例 JSON 文件:
[
{
"discordId": "9273927302020",
"characters": [
{
"name": "Rare_Character",
"value": 1
},
{
"name": "Ultra_Rare_Character",
"value": 1
}
]
}
]
举个例子,我 运行 这个简单的扭蛋得到了 4 个字符:
let i = 1
var picks = []
while(i <= 4){
const { pick } = gacha.simple(alpha)
picks.push(pick)
i++
}
现在,picks
有一个这样的数组:
[
{
"name": "Common_Character"
},
{
"name": "Ultra_Rare_Character"
},
{
"name": "Common_Character"
},
{
"name": "Rare_Character"
}
]
如何根据我在 gacha 结果 picks
中得到的 name
在我的示例 JSON 文件中增加 value
而忽略 Common_Character
并且只传递那些 Rare
和 Ultra_Rare
的?
我试过像这样过滤它们:
var filter = picks.filter(t => t.name === 'Rare_Character' || t.name === 'Ultra_Rare_Character')
现在我不知道如何增加我的 JSON 文件中的这些值,如果在 gacha 结果中我得到两个 Rare_Characters
或 Ultra_Rare_Character
怎么办?
我正在使用 fs
读取我的 JSON 文件,但我不知道增加值的逻辑
const src = [
{
"discordId": "9273927302020",
"characters": [
{
"name": "Rare_Character",
"value": 1
},
{
"name": "Ultra_Rare_Character",
"value": 1
}
]
}
];
const gacha = [
{
"name": "Common_Character"
},
{
"name": "Ultra_Rare_Character"
},
{
"name": "Common_Character"
},
{
"name": "Rare_Character"
}
];
const updateValues = (src, gacha) => {
const gachaSums = gacha.reduce((collector, current) => {
collector[current.name] = (collector[current.name] | 0) + 1;
return collector;
}, {});
src.characters.forEach(srcChar => {
gachaSums[srcChar.name] = srcChar.value + (gachaSums[srcChar.name] | 0);
});
src.characters = Object.entries(gachaSums).map(([key, value]) =>
({ name: key, value: value })
);
return src;
}
console.log(updateValues(src[0], gacha));
也许这个版本可以帮助
示例 JSON 文件:
[
{
"discordId": "9273927302020",
"characters": [
{
"name": "Rare_Character",
"value": 1
},
{
"name": "Ultra_Rare_Character",
"value": 1
}
]
}
]
举个例子,我 运行 这个简单的扭蛋得到了 4 个字符:
let i = 1
var picks = []
while(i <= 4){
const { pick } = gacha.simple(alpha)
picks.push(pick)
i++
}
现在,picks
有一个这样的数组:
[
{
"name": "Common_Character"
},
{
"name": "Ultra_Rare_Character"
},
{
"name": "Common_Character"
},
{
"name": "Rare_Character"
}
]
如何根据我在 gacha 结果 picks
中得到的 name
在我的示例 JSON 文件中增加 value
而忽略 Common_Character
并且只传递那些 Rare
和 Ultra_Rare
的?
我试过像这样过滤它们:
var filter = picks.filter(t => t.name === 'Rare_Character' || t.name === 'Ultra_Rare_Character')
现在我不知道如何增加我的 JSON 文件中的这些值,如果在 gacha 结果中我得到两个 Rare_Characters
或 Ultra_Rare_Character
我正在使用 fs
读取我的 JSON 文件,但我不知道增加值的逻辑
const src = [
{
"discordId": "9273927302020",
"characters": [
{
"name": "Rare_Character",
"value": 1
},
{
"name": "Ultra_Rare_Character",
"value": 1
}
]
}
];
const gacha = [
{
"name": "Common_Character"
},
{
"name": "Ultra_Rare_Character"
},
{
"name": "Common_Character"
},
{
"name": "Rare_Character"
}
];
const updateValues = (src, gacha) => {
const gachaSums = gacha.reduce((collector, current) => {
collector[current.name] = (collector[current.name] | 0) + 1;
return collector;
}, {});
src.characters.forEach(srcChar => {
gachaSums[srcChar.name] = srcChar.value + (gachaSums[srcChar.name] | 0);
});
src.characters = Object.entries(gachaSums).map(([key, value]) =>
({ name: key, value: value })
);
return src;
}
console.log(updateValues(src[0], gacha));
也许这个版本可以帮助