I have .json file, but since it is very big, I will paste an example of how it looks like:
[{"translations": {
"ces": {
"official": "Aruba",
"common": "Aruba"
},
"deu": {
"official": "Aruba",
"common": "Aruba"
},
"est": {
"official": "Aruba",
"common": "Aruba"
},
"fin": {
"official": "Aruba",
"common": "Aruba"
},
"fra": {
"official": "Aruba",
"common": "Aruba"
},
"hrv": {
"official": "Aruba",
"common": "Aruba"
},
"hun": {
"official": "Aruba",
"common": "Aruba"
},
"ita": {
"official": "Aruba",
"common": "Aruba"
},
"jpn": {
"official": "\u30a2\u30eb\u30d0",
"common": "\u30a2\u30eb\u30d0"
}}]
Original .json file contains more than 10 blocks like this. If you are curious, you can check the link: https://raw.githubusercontent.com/TheRadioDept/technical-question/main/countries.json
I also have javascript code that reads values from this .json file using require
method.
const enteredKey = process.argv.slice(2);
console.log('Key is : ', enteredKey);
/* Checking if number of parameters more than 0 */
/* Checking if entered translation key is supported by.json file. */
// eslint-disable-next-line max-len
if (enteredKey.length < 2 && enteredKey !== null && removeDuplicates(keys).includes(enteredKey[0])) {
try {
console.log(data.map(point => point.translations[0][enteredKey] ?
// eslint-disable-next-line max-len
point.translations[enteredKey].official: `No translation for ${ enteredKey }.`));
} catch (error) {
console.log('Cannot translate variable');
}
} else {
console.log('Incorrect key parameter');
}
When program runs, it takes an CLI parameter, let's say deu
for example and returns all parameters(official names`) of this key in .json file.
Please note that on my local PC both .json and JS files are stored in the same folder.
What I need is instead of doing one console.log for the whole array of keys
is to log(print) them one by one. I have tried using for loop
, but since I am unfamiliar with .json structures I don't know how to make it print it individually.
This is for loop
I tried:
for(i = 0; i < data.translations[enteredKey]; ++i) {
console.log(data.map(point => point.translations[enteredKey] ?
// eslint-disable-next-line max-len
point.translations[enteredKey].official: `No translation for ${ enteredKey }.`));
}
The output was console.log('Cannot translate variable');
.
Could anyone help please?