6 Replies Latest reply on Sep 17, 2015 5:39 PM by romanobstuder

    Multi Dimensional Array | Object

    romanobstuder Level 1

      Hi all

       

      I have created a script that traverses all tables in a document and collects text strings that are formatted with a character style "Footnote_Marker".

      (InDesign does not support Footnotes in Tables.)

       

      Every found entry gives me 2 parts:

      myPage (this is an object) and myFootnoteString (This is a string)

      var myPageName = myPage.name;   <----- This gives me a human readable page number

       

      The string can be something like "1" or "7" or "***"or "VISA only"

       

      Many entries of "1" could be found on Page X, (The footnote will always mean the same thing in the complete catalogue, for example 1 = Made in Italy

       

      So now I need to hold this data in a sensible construct.

       

      I would like to have only one Array Key for the pages with many Secondary Key entries for the string.

      Also if I find the Footnote marker "1" many times on the page it needs to go into the Array only once.

      And I need to be able to alpha sort the Footnotes.

       

      I already played with Arrays and Objects in Javascript but nothing works for me...

      Appreciate your help

       

      Regards

      Romano

        • 1. Re: Multi Dimensional Array | Object
          romanobstuder Level 1

          Here is some code I work with:

           

          var myPageFootnoteData = {};

           

           

          var myPage;

              var myFootnoteString;

           

           

              myPage = "aaaa";

              myFootnoteString = "AAAAA";

              addPageFootnote(myPageFootnoteData, myPage, myFootnoteString);

             

              myPage = "bbbb";

              myFootnoteString = "BBBBBB";

              addPageFootnote(myPageFootnoteData, myPage, myFootnoteString);

             

              myPage = "cccc";

              myFootnoteString = "CCCCCCCC";

              addPageFootnote(myPageFootnoteData, myPage, myFootnoteString);

             

              myPage = "dddd";

              myFootnoteString = "DD";

              addPageFootnote(myPageFootnoteData, myPage, myFootnoteString);

             

              myPage = "dddd";

              myFootnoteString = "12345";

              addPageFootnote(myPageFootnoteData, myPage, myFootnoteString);

             

              myPage = "dddd";

              myFootnoteString = "12345";

              addPageFootnote(myPageFootnoteData, myPage, myFootnoteString);

                      $.writeln("Checkpoint_02");

           

           

              outputDataArray(myPageFootnoteData);

           

           

          function addPageFootnote(myPageFootnoteData, myPage, myFootnoteString){

              $.writeln("This is my main object: " + myPageFootnoteData);

              $.writeln("This is the page: " + myPage);

              $.writeln("This is the footnote: " + myFootnoteString);

              //$.writeln("Cold call: show me the page: " + myPageFootnoteData[myPage].myPage);

              //var myPageHolder;

            var myPageHolder;

            var myFootnoteStringHolder;

              if(myPageHolder in myPageFootnoteData == false){

                  myPageFootnoteData[myPageHolder] = {};

           

           

                  $.writeln("It did not exist We insert this: " + myPage);

                  myPageFootnoteData[myPageHolder] = myPage;

           

           

                  $.writeln("OK show me the page: " + myPageFootnoteData[myPageHolder]);

              }

           

           

                  $.writeln("Checkpoint_01a");

           

           

              if (myFootnoteStringHolder in myPageFootnoteData == false) {

                  $.writeln("Checkpoint_01");

                  //$.writeln("2OK show me the page: " + myPageFootnoteData[myPage]);

                  myPageFootnoteData[myFootnoteStringHolder] = {};

                  }

                  if (myFootnoteStringHolder in myPageFootnoteData == false) {

                              $.writeln("Checkpoint_20");

           

           

                  }

          • 2. Re: Multi Dimensional Array | Object
            romanobstuder Level 1

            Here is a workable code.

            I can create the first outer Array (it is actually a raw Object).

            But the inner Array loop is not working at all.

            It is possible that I don't understand the Array concept at all. So if you can give me a helping hand that would be greatly appreciated.

            Regards

            Romano

             

             

             

             

             

             

             

             

             

            main();

            function main(){

                $.writeln("Checkpoint_0000");

                var myPageFootnoteData = {};

                myPageFootnoteDataFunction(myPageFootnoteData);

            }

             

             

            function myPageFootnoteDataFunction(myPageFootnoteData){

                $.writeln("This is the Object myPageFootnoteData: " + myPageFootnoteData);

                $.writeln("This is the Object myPageFootnoteData Length: " + myPageFootnoteData.length);

             

             

                var myPage;

                var myFootnoteString;

             

             

                myPage = "1";

                myFootnoteString = "AAAAA";

                addPageFootnote(myPageFootnoteData, myPage, myFootnoteString);

               

                myPage = "2";

                myFootnoteString = "BBBBB";

                addPageFootnote(myPageFootnoteData, myPage, myFootnoteString);

               

                myPage = "3";

                myFootnoteString = "CCCCC";

                addPageFootnote(myPageFootnoteData, myPage, myFootnoteString);

               

                myPage = "4";

                myFootnoteString = "DDDDD";

                addPageFootnote(myPageFootnoteData, myPage, myFootnoteString);

               

                myPage = "4";

                myFootnoteString = "12345";

                addPageFootnote(myPageFootnoteData, myPage, myFootnoteString);

               

                myPage = "4";

                myFootnoteString = "12345";

                addPageFootnote(myPageFootnoteData, myPage, myFootnoteString);

                        $.writeln("Checkpoint_02");

             

             

                outputDataArray(myPageFootnoteData);

                        $.writeln("Checkpoint_03");

             

             

                for(var a in myPageFootnoteData){

                    //$.writeln("[" + a + "]" + " ___ " + myDataArray[a].myPageName + " ___ " + myDataArray[a][a].myFootnoteString);

                        $.writeln("Array Key 01 and 02  Outside [" + myPageFootnoteData + "]" + " ___ " + myPageFootnoteData[a][b] + " ___ ");

                   for(var b in myPageFootnoteData[a]){

                        //$.writeln(string = string + myPageFootnoteData[a][b]+",");

                        $.writeln("Array Key 01 and 02  Inside [" + myPageFootnoteData[a] + "]" + " ___ " + myPageFootnoteData[a][b] + " ___ ");

                    }

                }

            }

             

             

             

             

             

             

             

             

            function addPageFootnote(myPageFootnoteData, myPage, myFootnoteString){

                $.writeln("This is my main object: " + myPageFootnoteData);

                $.writeln("This is the page: " + myPage);

                $.writeln("This is the footnote: " + myFootnoteString);

                var myPageHolder;

                var myFootnoteStringHolder;

                if(myPageHolder in myPageFootnoteData == false){

                    myPageFootnoteData[myPageHolder] = {};

                    $.writeln("It did not exist We insert this: " + myPage);

                    myPageFootnoteData[myPageHolder] = myPage;

                    $.writeln("OK show me the page: " + myPageFootnoteData[myPageHolder]);

                }

             

             

                $.writeln("Checkpoint_01a");

             

             

                if (myFootnoteStringHolder in myPageFootnoteData[myPageHolder] == false) {

                    $.writeln("Checkpoint_01");

                    //$.writeln("2OK show me the page: " + myPageFootnoteData[myPage]);

                    myPageFootnoteData[myPageHolder] = {};

                }

                if (myFootnoteStringHolder in myPageFootnoteData[myPageHolder] == false) {

                    $.writeln("Checkpoint_20");

                }

                //$.writeln("3OK show me the page: " + myPageFootnoteData[myPage]);

                //$.writeln("4: " + myPageFootnoteData[myPage][myFootnoteString]);

                //myPageFootnoteData[myPage][myFootnoteString] = myFootnoteString;

                //$.writeln("5: " + myPageFootnoteData[myPage][myFootnoteString]);

                //myPageFootnoteData[myPage][myFootnoteString] = myFootnoteString;  

                //$.writeln("Final test A: " + myPageFootnoteData[myPage]);

                //$.writeln("Final test B: " + myPageFootnoteData[myPage][myFootnoteString]);

            }   

             

             

             

             

            function outputDataArray(myPageFootnoteData){

                for(var a in myPageFootnoteData){

                    $.writeln("Array Key 01 [" + a + "]");

                                $.writeln("Checkpoint_10");

             

             

                    for(var b in myPageFootnoteData[a]){

                                    $.writeln("Checkpoint_11");

             

             

                        $.writeln("Array Key 02 [" + b + "]" );

                    }

                }

            }

            • 3. Re: Multi Dimensional Array | Object
              Peter Kahrel Adobe Community Professional & MVP

              Hi Romano,

               

              Maybe you could use a method along these lines:

               

              Two objects: 'known' to keep track of notes placed on a page and and 'obj' to store your data

               

              known = {};
              obj = {};
              

               

              For every found object, (a) if the page doesn't exist as a key, add a key, its value will be the string as a single-element array; (b) if the page key exists, add the string to the array if it's not yet in the array:

               

              if (obj[myPage] === undefined) {
                 obj[myPage] = [myString];
              } else {
                 if (!known[myPage+'/'+myString]) {
                    obj[myPage].push(myString);
                    known[myPage+'/'+myString] = true;
                 }
              }
              

               

              Now you have an object with unsorted keys/page numbers, and each key has an array of unsorted arrays. You can't sort objects, so transfer it to an array. And while you're doing this, sort the arrays:

               

              arr = [];
              for (i in obj) {
                 arr.push ({page: Number(i), notes: obj[i].sort()});
              }
              

               

              Then sort the array by page number:

               

              arr.sort (function(a,b) {return a.page-b.page});
              

               

              Hope this helps

               

              Peter

              • 4. Re: Multi Dimensional Array | Object
                romanobstuder Level 1

                Hi Peter

                Thank you very much!

                 

                I still try to understand what happens when we go into the arr

                 

                I guess page and notes is what I am looking for.

                 

                How do I know travers and output the arr data?

                I end up with the keys, not the data that they hold, I think

                 

                Regards

                Romano

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                main();

                function main(){

                    $.writeln("Checkpoint_0000");

                    var known = {};

                    var obj = {};

                 

                 

                    myPageFootnoteDataFunction(known, obj);

                    mySortPageFootnote(known, obj);

                 

                 

                }

                 

                 

                function myPageFootnoteDataFunction(known, obj){

                 

                 

                    var myPage;

                    var myFootnoteString;

                 

                 

                    myPage = "1";

                    myFootnoteString = "AAAAA";

                    addPageFootnote(known, obj, myPage, myFootnoteString);

                   

                    myPage = "2";

                    myFootnoteString = "BBBBB";

                    addPageFootnote(known, obj, myPage, myFootnoteString);

                   

                    myPage = "3";

                    myFootnoteString = "CCCCC";

                    addPageFootnote(known, obj, myPage, myFootnoteString);

                   

                    myPage = "4";

                    myFootnoteString = "DDDDD";

                    addPageFootnote(known, obj, myPage, myFootnoteString);

                   

                    myPage = "4";

                    myFootnoteString = "ddd";

                    addPageFootnote(known, obj, myPage, myFootnoteString);

                   

                    myPage = "4";

                    myFootnoteString = "12345";

                    addPageFootnote(known, obj, myPage, myFootnoteString);

                   

                    myPage = "4";

                    myFootnoteString = "12345";

                    addPageFootnote(known, obj, myPage, myFootnoteString);

                 

                 

                 

                 

                }

                 

                 

                 

                 

                 

                 

                 

                 

                function addPageFootnote(known, obj, myPage, myFootnoteString){

                    $.writeln("This is the page: " + myPage);

                    $.writeln("This is the footnote: " + myFootnoteString);

                 

                 

                    if (obj[myPage] === undefined) {

                        obj[myPage] = [myFootnoteString];

                    } else {

                        if (!known[myPage+'/'+myFootnoteString]) {

                            obj[myPage].push(myFootnoteString);

                            known[myPage+'/'+myFootnoteString] = true;

                        }

                    }

                    $.writeln("2OK show me the page: " + obj[myPage]);

                    $.writeln("3OK show me the page: " + known[myPage+'/'+myFootnoteString]);

                 

                 

                }   

                 

                 

                function mySortPageFootnote(known, obj){

                    arr = [];

                    for (i in obj) {

                        arr.push ({page: Number(i), notes: obj[i].sort()});

                    }

                 

                 

                    arr.sort (function(a,b) {return a.page-b.page});

                    outputDataArray(arr);

                 

                 

                }   

                 

                 

                 

                 

                function outputDataArray(arr){

                    for(var a in arr){

                        $.writeln("Array Key 01 [" + a + "]");       

                        for(var b in arr[a]){

                            $.writeln("Array Key 02 [" + b + "]" );          

                            for(var c in arr[a][b]){

                                $.writeln("Array Key 03 [" + c + "]" );

                            }

                        }

                    }

                }

                • 5. Re: Multi Dimensional Array | Object
                  Peter Kahrel Adobe Community Professional & MVP

                  You could write the array like this:

                   

                  for (i = 0; i < arr.length; i++) {
                     $.writeln ('Page: ' + arr[i].page + ', notes: ' + arr[i].notes.join(', '));
                  }
                  

                   

                  P.

                  • 6. Re: Multi Dimensional Array | Object
                    romanobstuder Level 1

                    Super! I had no idea about the .join.

                    Thank you very much for this. All the best and talk soon.

                    Regards

                    Romano