json 从通过fs导入的数组中获取所有项

qcuzuvrc  于 2022-11-19  发布在  其他
关注(0)|答案(1)|浏览(111)

我需要从jsonParsed.students[actStudent].evnetsPartaken中获取值,这是一个数组,例如[1,2,0]
(例如,非完整数据,请检查附件中的完整数据)

{
    "students":
    [

    { 
        
        "firstName":"John", 
             "lastName":"Doe", 
             "gradeNum":"9",
             "serviceHours":99.00,
             "studentNotes":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sem magna, commodo nec enim quis.",
             "evnetsPartaken":[1,3.1] 
            },
    { 
        
        "firstName":"SamSame", 
             "lastName":"SamSame", 
             "serviceHours":234,
              "studentNotes":":trollface.jpg:",
             "evnetsPartaken":[1,3] 
            },
    {

每个值都与configEvents json数组中的event数组编号相关
同样,这不是完整的数据集

[
"configEvents":
  [

  { 
      
     
           "eventName":"Football", 
           "isSport": true,
           "eventTags":["exampleTag1","exampleTag2"],
           "evnetDates":[13934823420,12892839483,23642834823 ]
          },
  { 
      
       
           "eventName":"Softball", 
           "isSport": true,
           "eventTags":["exampleTag1","exampleTag2"],
           "evnetDates":[13934823420,12892839483,23642834823 ]
          },
          { 
      
           "eventName":"Spirt Rally", 
           "isSport": false,
           "eventTags":["inSchool","exampleTag2"],
           "evnetDates":[878687686868 ]
          },
  { 
      
           "eventName":"Grade Party", 
           "isSport": false,
           "eventTags":["inSchool","exampleTag2"],
           "evnetDates":[82832497686868 ]
          } 
]

我不知道如何获取数组值jsonParsed.students[actStudent].evnetsPartaken[]

fs.readFile('./testdata.json', 'utf8', (error, data) => {
     if(error){
        console.log(error);
        return;
     }

     //Store the json data output
     var jsonData = data;
     //Store a JSON Parsed version of the data (object.based )
     var jsonParsed = JSON.parse(jsonData)
    //One liner made by Dinesh Soni to make it easier to extracted values from json objects, example and article can be found in readme


     console.log(jsonParsed);
     console.log(jsonParsed.students);
     var actStudent = 1;
     var actEvents = 
     console.log(jsonParsed.students[actStudent].firstName + " " + jsonParsed.students[actStudent].lastName + " Partakes in " + (
      jsonParsed.configEvents[
      jsonParsed.students[1].evnetsPartaken[] //issue is here (this is the said arguments)
    ].eventName
      ));

})

如果我自己输入参数,它可以正常工作,但这不是解决方案。是否有适当的方法来获得这些值,应该注意的是,actStudent只是用于测试,在真实的程序中会有很大变化
我得把它放进去

jsonParsed.configEvents[jsonParsed.students[1].evnetsPartaken[]].eventName

因为.students[1]. evnetsPartaked []会告诉它要使用的事件数组,然后括号外的所有项目都会使用该数据来取得正确的事件信息,例如jsonParsed.configEvents[jsonParsed.students[1]. evnetsPartaked [0]].eventName
would return Softball because it would corelate to configEvents[1].eventName which is Softball
1.在我例子中

"evnetsPartaken":[1,3]

如果输入为[0],则会得到第一个参数,因为jsons从零开始计数
1.完成此操作后,您将获得事件数组[1],即垒球
testdata.json on pastecord
main.js on pastecord
索引文件不是一个解决方案,将只是一个临时修复。我尝试做一个for循环,但我得到了语法错误时,这样做。ps我是新的电子,所以我可能会做一些非常愚蠢的。这是我的第一个项目,它的...由于箭头,所以我没有太多的时间。

vuktfyat

vuktfyat1#

感谢你的评分
map将它们作为数组提供给我,我只选择了eventName,就像代码应该做的那样

console.log(jsonParsed.students[actStudent].firstName + " " + jsonParsed.students[actStudent].lastName + " Partakes in " + (
    jsonParsed.students[actStudent].evnetsPartaken.map(evnet => jsonParsed.configEvents[evnet].eventName).join(`, `)
));

相关问题