Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Multi Dimensional Array | Object

Explorer ,
Sep 16, 2015 Sep 16, 2015

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

TOPICS
Scripting
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 17, 2015 Sep 17, 2015

You could write the array like this:

for (i = 0; i < arr.length; i++) {

   $.writeln ('Page: ' + arr.page + ', notes: ' + arr.notes.join(', '));

}

P.

Translate
Explorer ,
Sep 16, 2015 Sep 16, 2015

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");

        }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 16, 2015 Sep 16, 2015

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.myPageName + " ___ " + myDataArray.myFootnoteString);

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

       for(var b in myPageFootnoteData){

            //$.writeln(string = string + myPageFootnoteData+",");

            $.writeln("Array Key 01 and 02  Inside [" + myPageFootnoteData + "]" + " ___ " + 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);

    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){

                        $.writeln("Checkpoint_11");

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

        }

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 17, 2015 Sep 17, 2015

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.sort()});

}

Then sort the array by page number:

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

Hope this helps

Peter

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 17, 2015 Sep 17, 2015

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.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){

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

            for(var c in arr){

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

            }

        }

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 17, 2015 Sep 17, 2015

You could write the array like this:

for (i = 0; i < arr.length; i++) {

   $.writeln ('Page: ' + arr.page + ', notes: ' + arr.notes.join(', '));

}

P.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 17, 2015 Sep 17, 2015
LATEST

Super! I had no idea about the .join.

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

Regards

Romano

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines