Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
How to get a single value and value name from an JSON file without knowing the value name

I have a discord bot and it saves achievements in a JSON file. The JSON structure is like this:

{
  "784095768305729566": {
    "coins": 14598,
    "achievements": {
      "taking_inventory": true
    }
  },
}

The command should give you an overview of what achievements you already have.

I want to create an embed and run a for loop for every sub-thing of achievements. If the value is true, the for loop should take the value name and the value and add a field to the embed where the field title is the value name.

I have multiple problems there.

  1. I don't know how to get value names and values. I already tried Object.keys(...) but that gives all keys and not one by one. I don't know how to get the values.
  2. I don't know how to make the for loop as long as all sub-things of achievements.

I tried:

for(var i = 0; i<datafile[id].achievements.length; i++){...}

but that didn't work wither.

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

You can get an array of an object's entries (keys and values) from Object.entries. You can filter that array for the value to be true You can map the result to the key. This gives you an array of achievement keys which had the value "true".

const datafile = {
  "784095768305729566": {
    "coins": 14598,
    "achievements": {
      "taking_inventory": true,
      "other_achievement": false
    }
  },
};

const id = "784095768305729566";

const achievements = Object.entries(datafile[id].achievements)
  .filter(([k, v]) => v)
  .map(([k, v]) => k);

// do something with achievements
console.log(achievements);

about 4 years ago · Santiago Gelvez Relatório

0

You can use Object.entries that returns an array of a given object's own enumerable string-keyed property [key, value] pairs:

let user = "784095768305729566"
let obj = {
  "784095768305729566": {
    "coins": 14598,
    "achievements": {
      "taking_inventory": true,
      "another_achievement": true,
      "yet_another_achievement": false,
      "and_one_more": true,
    }
  },
}

let fields = Object.entries(obj[user].achievements)
  .map(([name, value]) => ({
    name,
    value: value ? '✅' : '❌',
    inline: false,
  }))

console.log(fields)

// or if you only want to include achievements a user already has

let onlyTruthyFields = Object.entries(obj[user].achievements)
  // only where value is truthy
  .filter(([name, value]) => value)
  .map(([name, value]) => ({
    name,
    value: '✅',
    inline: false,
  }))

console.log(onlyTruthyFields)

And then just add these to your embed:

embed.addFields(fields)
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda