Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - How can i import only discord from JSON file?

I've got an array of objects, these objects have an identifiers array with strings. One of these strings contains a 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.

enter image description here

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. Click on image to see more. There's json file there. It's all about ${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}
u200b
`,true);
        // .addField('u200b','u200b
u200b
',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' : '
u200f'}`;
          // }
          
          fields[0] = `**Mieszkańcy na wyspie:**
`;
          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',`
${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();
    }
  };
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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.

Luckily, the element always start with the word 'discord', so you can use Array#find() with String#startsWith() to find your discord 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}
  `)
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...