[英]How can i import only discord from JSON file?
I've got an array of objects, these objects have an identifiers
array with strings.我有一个对象数组,这些对象有一个带有字符串的
identifiers
数组。 One of these strings contains a Discord ID.其中一个字符串包含一个 Discord ID。 The problem is its position is changing all the time, so I can't use the 4th identifier for example.
问题是它的位置一直在变化,所以我不能使用第四个标识符。
I only need the discord property.我只需要不和谐属性。
I tried many things.我尝试了很多东西。 I think it should be something like only "discord" property, but I have no idea how to solve that and code that up.
我认为它应该只是“discord”属性,但我不知道如何解决这个问题并对其进行编码。 Click on image to see more.
点击图片查看更多。 There's json file there.
那里有json文件。 It's all about
${players[i].identifiers[4]
这都是关于
${players[i].identifiers[4]
const updateMessage = function() {
getVars().then((vars) => {
getPlayers().then((players) => {
if (players.length !== LAST_COUNT) log(LOG_LEVELS.INFO,`${players.length} graczy`);
let queue = vars['Queue'];
let embed = UpdateEmbed()
.addField('Status serwera','<:tak:847199217063297056> Online',true)
.addField('W kolejce',queue === 'Enabled' || queue === undefined ? '0' : queue.split(':')[1].trim(),true)
.addField('Graczy online',`${players.length}/${MAX_PLAYERS}\n\u200b\n`,true);
// .addField('\u200b','\u200b\n\u200b\n',true);
if (players.length > 0) {
// method D
const fieldCount = 3;
const fields = new Array(fieldCount);
fields.fill('');
// for (var i=0;i<players.length;i++) {
// fields[i%4 >= 2 ? 1 : 0] += `${players[i].name}${i % 2 === 0 ? '\u200e' : '\n\u200f'}`;
// }
fields[0] = `**Mieszkańcy na wyspie:**\n`;
for (var i=0;i<players.length;i++) {
fields[(i+1)%fieldCount] += `${players[i].name} [${players[i].id}], ${players[i].identifiers[4]}`;
}
for (var i=0;i<fields.length;i++) {
let field = fields[i];
if (field.length > 0) embed.addField('\u200b',`\n${fields[i]}`, true);
}
}
sendOrUpdate(embed);
LAST_COUNT = players.length;
}).catch(offline);
}).catch(offline);
TICK_N++;
if (TICK_N >= TICK_MAX) {
TICK_N = 0;
}
for (var i=0;i<loop_callbacks.length;i++) {
let callback = loop_callbacks.pop(0);
callback();
}
};
If I understand you correctly, you want to get the element from the identifiers
array, but its position is not fixed, it can be anywhere.如果我理解正确,您想从
identifiers
数组中获取元素,但它的位置不是固定的,它可以在任何地方。
Luckily, the element always start with the word 'discord', so you can use Array#find()
with String#startsWith()
to find your discord ID.幸运的是,该元素始终以单词“discord”开头,因此您可以使用
Array#find()
和String#startsWith()
来查找您的不和谐 ID。 Check out the snippet below:看看下面的片段:
let players = [{ id: 1, identifiers: ["license:57349756783645", "xbl:85437852", "live:8953291341", "discord:89325813521519", "fivem:893123"], name: "foo", ping: 56 }, { id: 2, identifiers: ["xbl:57420987", "live:09123489", "discord:86543932136453", "license:865496782134", "fivem:584723"], name: "bar", ping: 41 }, { id: 3, identifiers: ["live:99532945", "discord:80521578413532", "license:60795634523", "xbl:1239435", "fivem:943921"], name: "bar", ping: 41 }] players.forEach(player => { let discord = player.identifiers.find(i => i.startsWith('discord')).replace('discord:', '') console.log(` ID: ${player.id} Name: ${player.name} Discord: ${discord} `) })
// ...
fields[0] = `**Mieszkańcy na wyspie:**\n`;
for (var i = 0; i < players.length; i++) {
const discord = players[i].identifiers.find((el) =>
el.startsWith('discord'),
).replace('discord:', '');
fields[
(i + 1) % fieldCount
] += `${players[i].name} [${players[i].id}], ${discord}`;
}
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.