[英]How can I print a specific part of a JSON object with JavaScript?
I would like to know what to use to transform the document from JSON
to an array and the print the part of the array the user wants. 我想知道用什么将文档从
JSON
转换为数组,并打印用户想要的数组部分。 Also, how can I put it in an HTML
document so the user can search any part of the array. 另外,如何将其放在
HTML
文档中,以便用户可以搜索数组的任何部分。
Below is the JSON
. 以下是
JSON
。
{ "A": { "1": { "1\º": [ "Semestre 1" ] }, "2": { "1\º": [ "Semestre 1" ] } }, "B": [ ], "c": { "2": { "1\º": [ "Semestre 1" ] }, "3": { "1\º": [ "Semestre 1" ] }, "44": { "1\º": [ "Semestre 1" ] }, "G6": { "1\º": [ "Semestre 1" ] }, "GP98": { "1\º": [ "Semestre 1" ] }, "654": { "1\º": [ "Semestre 1" ] }, "5556": { "1\º": [ "Semestre 1" ] }, "7654": { "1\º": [ "Semestre 1" ] } } }
Thanks for your help. 谢谢你的帮助。
Depending on what you want your strings/collection of strings to look like you can do one of the below options. 根据您希望您的字符串/字符串集合看起来像什么,可以执行以下选项之一。
JSON.Stringify will convert a JavaScript object or value to a JSON string. JSON.Stringify将JavaScript对象或值转换为JSON字符串。
printDataWithStringify = (x) => {
console.log(JSON.stringify(data[x]))
}
> {"1":{"1º":["Semestre 1"]},"2":{"1º":["Semestre 1"]}}
If you want to go even further, you can try the below code. 如果您想更进一步,可以尝试以下代码。
var printedStrings = []
checkNestedData = (x, y) => {
if (typeof(x[y]) === 'object' && !Array.isArray(x[y][property1])) {
printedStrings.push(y)
for (var property1 in x[y]) {
checkNestedData(x[y], property1)
}
} else {
printedStrings.push(x[y]);
}
}
printDataWithKeysAndValues = (x) => {
var part = data[x]
for (var property1 in part) {
checkNestedData(part, property1)
}
console.log(printedStrings)
}
> 1,1º,Semestre 1,2,1º,Semestre 1
The above code utilizes a for...in
loop which is used to loop through JavaScript objects. 上面的代码利用了
for...in
循环,该循环用于遍历JavaScript对象。 part
is the object you get when you retrieve information from data
at the key of x
(in this case, "A"
). part
是从x
的键(在本例中为"A"
)中的data
中检索信息时获得的对象。 property1
represents the key for the current object ( part
) and is the iterator for the for...in
loop as we loop through part
. property1
表示当前对象( part
)的键,并且是我们遍历part
for...in
循环的迭代器。
To take it even further, checkNestedData
checks to see if there is another object nested in the current object. 为了更进一步,
checkNestedData
检查是否在当前对象中嵌套了另一个对象。 If it's an object (that doesn't have a array for a child), you push y
(remember this is the key ( property1
) from the original loop) into the intialized printedStrings
array. 如果它是一个对象(没有用于孩子的数组),则将
y
(请记住,这是原始循环中的键( property1
))推入初始化的printedStrings
数组中。 Then perform another loop and you run checkNestedData
recursively on the current iterative in the new loop. 然后执行另一个循环,然后对新循环中的当前迭代递归运行
checkNestedData
。
This recursive callback will run until the last child is not a populated object. 此递归回调将一直运行,直到最后一个子代不是填充对象为止。
Once we've looped through the entire object, storing the keys and values we've extracted from the object (including nested objects) we simply console.log
the final array which contains all of our keys and value from that portion ( "A"
) of data. 一旦遍历整个对象,存储从对象中提取的键和值(包括嵌套对象),我们就可以简单地
console.log
最终数组,其中包含该部分中所有的键和值( "A"
) 数据的。
Depending on how you want the string to be formatted, you can append/remove things from the strings via interpolation or concatenation. 根据您希望字符串格式化的方式,您可以通过插值或串联从字符串中追加/删除内容。 But this solution should get you every key and value and store them as a string in an array.
但是此解决方案应该为您提供每个键和值,并将它们作为字符串存储在数组中。
var data = { "A": { "1": { "1\º": [ "Semestre 1" ] }, "2": { "1\º": [ "Semestre 1" ] } }, "B": [ ], "c": { "2": { "1\º": [ "Semestre 1" ] }, "3": { "1\º": [ "Semestre 1" ] }, "44": { "1\º": [ "Semestre 1" ] }, "G6": { "1\º": [ "Semestre 1" ] }, "GP98": { "1\º": [ "Semestre 1" ] }, "654": { "1\º": [ "Semestre 1" ] }, "5556": { "1\º": [ "Semestre 1" ] }, "7654": { "1\º": [ "Semestre 1" ] } } } printDataWithStringify = (x) => { console.log('STRINGIFY: ' + JSON.stringify(data[x])) } var printedStrings = [] checkNestedData = (x, y) => { if (typeof(x[y]) === 'object' && !Array.isArray(x[y][property1])) { printedStrings.push(y) for (var property1 in x[y]) { checkNestedData(x[y], property1) } } else { printedStrings.push(x[y]); } } printDataWithKeysAndValues = (x) => { var part = data[x] for (var property1 in part) { checkNestedData(part, property1) } console.log('ALL KEYS AND VALUES: ' + printedStrings) } printDataWithStringify("A") printDataWithKeysAndValues("A")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.