Multiple tables in a for loop

Viewed 97

Hello dear stackoverflow community,

I want to make an Electron app [Javascript not jQuary] (or am in the process of doing so) and would like to add a function that puts one config into the "format" of another.

The big file from which I want to take the information I currently read in via "dialog.showOpenDialog" and can also access the json object.

Now to the problem:

The file I get via the dialog is 8000 lines long and contains individual information that I want to pack into a smaller document with about 3000 lines. Important: Individual information have a different name e.g. I want "ABCD: 23" from document 1 in the other as EFG: 23.

I had already asked this question and also received a good answer from a user[click here] which also works well, however I have now copied and pasted the function several times (as below in the example with the for function)

And this is of course not the fine English way... is there a way to make this more compact?

BigCFG.json:

"ANIMALS"{
        "DOG": {
            "DOG1": "0" ,
            "DOG2": "1" ,
            "BREED": {
               "breed": "1",
               "breed": "2",
            },
        },
        "CAT": {
            "CAT1": "0",
            "CAT2": "1" ,
            "BREED": {
               "breed": "1",
               "breed": "2",
            },
        },
}

Tables:

let dogTable = {
'dog1':'dog2',
'dog3': 'dog4'
}

let dogbreedTable = {
'breed1':'breed2',
'breed3': 'breed4'
}

let catTable = {
'cat1':'cat2',
'cat3': 'cat4'
}
 let catbreedTable = {
'breed1':'breed2',
'breed3': 'breed4'
}

The for function which I would like to have more compact: (I have the currently about 40 times in my project in a row to test)

for (let bigKey in BigCFG.ANIMALS.DOG) {
  if (bigKey in dogTable) {
  smallCFG.ANIMALS.DOG[dogTable[bigKey]] = BigCFG.ANIMALS.DOG[bigKey]
                            }
for (let bigKey in BigCFG.ANIMALS.DOG.BREED) {
  if (bigKey in dogbreedTable) {
  smallCFG.ANIMALS.DOG.BREED[dogbreedTable[bigKey]] = BigCFG.ANIMALS.DOG.BREED[bigKey]
                            }
for (let bigKey in BigCFG.CAT) {
  if (bigKey in catTable) {
  smallCFG.ANIMALS.CAT[catTable[bigKey]] = BigCFG.ANIMALS.CAT[bigKey]
                            }
for (let bigKey in BigCFG.ANIMALS.CAT.BREED) {
  if (bigKey in catbreedTable) {
  smallCFG.ANIMALS.CAT.BREED[catbreedTable[bigKey]] = BigCFG.ANIMALS.CAT.BREED[bigKey]
                            }

Expected smallCFG.json:

"ANIMALS": {
    "CAT": {
         "cat": "3",
         "cat": "4",
        "BREED": {
           "breed": "2",
           "breed": "3",
        },
    },
    "DOG": {
         "dog": "3",
         "dog": "4",
        "BREED": {
           "breed": "3",
           "breed": "4",
        },
    }
}

I would be very grateful for the help and of course appreciate your time

1 Answers

Not sure if I fully understood your problem and constraints. What I get by your other questions is you need to transform a json document for one structure to another, right? Have you tried JSONata? Maybe it matches your requirements.

PS.: I've tried add this as a comment, but I don't have reputation enough

Related