【发布时间】:2022-11-16 15:28:44
【问题描述】:
**Explanation**
I need to be able to get the values from
`jsonParsed.students[actStudent].evnetsPartaken`
Which is an array and would be for example` [1,2,0]`
(example, not full data, check attachments for full data)
```json
{
"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]
},
{
每个值与configEventsjson数组中事件事件的数组编号相关
同样,这不是完整的数据集
[
"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 ]
}
]
**Issue**
I can't figure how how to get the array values `jsonParsed.students[actStudent].evnetsPartaken[]`
```js
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].evnetsPartaken[] 告诉它要使用什么事件数组,然后括号外的所有内容都使用该数据来获取正确的事件信息 例如 jsonParsed.configEvents[jsonParsed.students[1].evnetsPartaken[0]].eventName
would return Softball because it would corelate to configEvents[1].eventName which is Softball
-
在我的例子中
"evnetsPartaken":[1,3]输入 [0] 将获得第一个参数,因为您从零开始计算 jsons
-
这样做之后你会得到 Event array [1] 这是 Softball
testdata.json on pastecord
main.js on pastecord索引文件不是解决方案,只是临时修复。我尝试做一个 for 循环,但在做时出现语法错误。 ps 我是电子新手,所以我可能正在做一些非常愚蠢的事情。这是我的第一个项目,它...由于骨髓所以我没有太多时间。
【问题讨论】:
标签: javascript node.js arrays json electron