简体   繁体   English

如何在不知道值名的情况下从 JSON 文件中获取单个值和值名

[英]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.我有一个不和谐的机器人,它将成就保存在 JSON 文件中。 The JSON structure is like this: JSON结构是这样的:

{
  "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.我想为成就的每个子事物创建一个嵌入并运行一个 for 循环。 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.如果值为true ,则 for 循环应获取值名称和值,并将字段添加到嵌入中,其中字段标题是值名称。

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.我已经尝试过Object.keys(...)但这给出了所有的键,而不是一个一个。 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 .只要achievements的所有子事物,我都不知道如何制作for循环。

I tried:我试过了:

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

but that didn't work wither.但这并没有消退。

You can get an array of an object's entries (keys and values) from Object.entries.您可以从 Object.entries 获取对象条目(键和值)的数组。 You can filter that array for the value to be true You can map the result to the key.您可以过滤该数组以使值为 true 您可以将结果映射到键。 This gives you an array of achievement keys which had the value "true".这为您提供了一个值为“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);

You can use Object.entries that returns an array of a given object's own enumerable string-keyed property [key, value] pairs:您可以使用Object.entries返回给定对象自己的可枚举字符串键属性[key, value]对的数组:

 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM