How to make this Javascript code more compact?

Started by
3 comments, last by a light breeze 1 year, 2 months ago

Is there an easier way to make the following code?


                switch(ids[1][i][0][1]) {
                    case ids[0][3]:
                        ids[0][2].push([subInpVal,subTxtArray,[],ids[1][i][0][2]]);
                        break;
                    case ids[0][2][3]:
                        ids[0][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][3]:
                        ids[0][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][3]:
                        ids[0][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][2][3]:
                        ids[0][2][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][2][2][3]:
                        ids[0][2][2][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][2][2][2][3]:
                        ids[0][2][2][2][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][2][2][2][2][3]:
                        ids[0][2][2][2][2][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][2][2][2][2][2][3]:
                        ids[0][2][2][2][2][2][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][2][2][2][2][2][2][3]:
                        ids[0][2][2][2][2][2][2][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][2][2][2][2][2][2][2][3]:
                        ids[0][2][2][2][2][2][2][2][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][2][2][2][2][2][2][2][2][3]:
                        ids[0][2][2][2][2][2][2][2][2][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][2][2][2][2][2][2][2][2][2][3]:
                        ids[0][2][2][2][2][2][2][2][2][2][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][2][2][2][2][2][2][2][2][2][2][3]:
                        ids[0][2][2][2][2][2][2][2][2][2][2][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][2][2][2][2][2][2][2][2][2][2][2][3]:
                        ids[0][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][3]:
                        ids[0][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][3]:
                        ids[0][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                    case ids[0][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][3]:
                        ids[0][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2]=[subInpVal,subTxtArray,[],ids[1][i][0][2]];
                        break;
                }

This basically builds an array, like the following:

[
  "some text",
  [
    "could be an array, but not always."
  ],
  [
    [
      "",
      "",
      []
    ],
    [
      "",
      "",
      [
        [
          "",
          "",
          [
            [
              "",
              "",
              [
                [
                  "",
                  "",
                  ["You can see the picture."]
                ]
              ]
            ]
          ]
        ]
      ]
    ]
  ]
]

However, the way that I have it calling and creating the array is just difficult, so I want to make it better, so that I don't have to have so many lines of code, anyways I only need it to go 17 deep from the start array. Is it possible to make it more compact? I am okay with using jQuery.

– Erik P. Kountzman - Owner - of - Airent Animation Entertainment --

Advertisement

How did you end up with that data structure? This doesn't seem normal.

Can I guess - you always have some janky nested arrays as an imput; and they mostly have empty elements / not very useful nesting. In this case, the task at hand condenses to recursively traversing nested arrays and extracting “leaf” non-empty strings - right?

None

Can you post samples of possible inputs and the respective desired outputs? What doesn't “seem normal” to me is that the same monster array nest ids is both input and output, and that it doesn't contain useful identifiers.

Omae Wa Mou Shindeiru

Looks like what you're looking for is a loop similar to the following:

for (x = ids[0]; x !== ids[0][2][2][2][2][2][2][2][2][2][2][2]; x = x[2]) {
  if (x[3] === ids[1][i][0][1]) {
    x[2] = [subInpVal,subTxtArray,[],ids[1][i][0][2]];
    break;
  }
}

This topic is closed to new replies.

Advertisement