To achieve the desired format for your strings/collection of strings, you have a few options available.
JSON.Stringify can be used to convert a JavaScript object or value to a JSON string.
printDataWithStringify = (x) => {
console.log(JSON.stringify(data[x]))
}
> {"1":{"1º":["Semestre 1"]},"2":{"1º":["Semestre 1"]}}
If you want to delve deeper into this, you may consider utilizing the following code snippet.
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 makes use of a for...in
loop to iterate over JavaScript objects. Here, part
represents the object obtained when extracting information from data
at key x
. The variable property1
serves as the key for the current object and acts as the iterator for the loop through part
.
In addition, the function checkNestedData
examines whether there is another nested object within the current object. If an object (excluding those with arrays as children) is encountered, it adds the key (y
) to the defined printedStrings
array. The function then recursively calls itself on the current iteration of the new loop.
This recursive process continues until the last child is not an populated object.
Upon completion of looping through the entire object and storing the extracted keys and values (including nested objects), the final array containing all the keys and values for that portion ("A"
) of data is displayed using console.log
.
Based on your formatting preferences, you can further modify the strings by using interpolation or concatenation. However, this solution effectively captures every key and value, storing them as strings in an array.
var data = {
"A": {
"1": {
"1\u00ba": [
"Semestre 1"
]
},
"2": {
"1\u00ba": [
"Semestre 1"
]
}
},
"B": [
],
"c": {
"2": {
"1\u00ba": [
"Semestre 1"
]
},
"3": {
"1\u00ba": [
"Semestre 1"
]
},
"44": {
"1\u00ba": [
"Semestre 1"
]
},
"G6": {
"1\u00ba": [
"Semestre 1"
]
},
"GP98": {
"1\u00ba": [
"Semestre 1"
]
},
"654": {
"1\u00ba": [
"Semestre 1"
]
},
"5556": {
"1\u00ba": [
"Semestre 1"
]
},
"7654": {
"1\u00ba": [
"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")