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

What is the best way to compare 2 arrays?

Explorer ,
May 21, 2018 May 21, 2018

Copy link to clipboard

Copied

I am filtering through a lot of text frames to get some numbers. The numbers reside on 2 different layers.

So I have gathered just the numbers from each layer and given them each their own array.

I would like to compare the 2 arrays and if something is missing from either array have a text frame created under the correct header as a list of what is missing from each array (if any is missing at all).

So compare the sorted arrays....

tableCallouts to graphicCallouts

Any thoughts on the best method of doing this? There could also be duplicates in either list to throw another wrench into things

#target illustrator-22

var doc = app.activeDocument;

/* --------- Text filtering for the Tables layer --------- */

var tableText = doc.layers['Tables'].textFrames;

// Setup array to take data from loop

var tableCallouts = [];

// Loop to only find callout numbers

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

    var checkNumber = parseInt(tableText.contents);

    if (isNaN(checkNumber) == false) {

        //alert("Found a number " + tableText.contents);

        tableCallouts.push(tableText.contents);

    }

}

// Sort the array

tableCallouts.sort();

/* --------- Text filtering for the Callouts/CONNs layer --------- */

var graphicText = doc.layers['Callouts/CONNs'].textFrames;

// Setup array to take data from loop

var graphicCallouts = [];

// Loop to only find callout numbers

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

    var checkNumber = parseInt(graphicText.contents);

    if (isNaN(checkNumber) == false) {

        graphicCallouts.push(graphicText.contents);

    }

}

// Sort the array

graphicCallouts.sort();

var origin = doc.rulerOrigin

var graphicHeaderText = doc.textFrames.add();

graphicHeaderText.textRange.characterAttributes.textFont = app.textFonts.getByName("ArialMT");

graphicHeaderText.position = [-500, 0];

graphicHeaderText.contents = "MISSING FROM GRAPHICS"

var tableHeaderText = doc.textFrames.add();

tableHeaderText.textRange.characterAttributes.textFont = app.textFonts.getByName("ArialMT");

tableHeaderText.position = [-300, 0];

tableHeaderText.contents = "MISSING FROM TABLE"

TOPICS
Scripting

Views

488

Translate

Translate

Report

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

Engaged , May 22, 2018 May 22, 2018

My suggestion would be to clean up both arrays first from any double entries. After that you loop through the first array and compare each entry with the content of the second array.

function unique(ain) {  

         var seen = {}  

         var aout = []  

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

             var elt = ain  

             if (!seen[elt]) {  

                aout.push(elt)  

                seen[elt] = true  

                }  

             }  

          return aout

}

This

...

Votes

Translate

Translate
Adobe
Engaged ,
May 22, 2018 May 22, 2018

Copy link to clipboard

Copied

My suggestion would be to clean up both arrays first from any double entries. After that you loop through the first array and compare each entry with the content of the second array.

function unique(ain) {  

         var seen = {}  

         var aout = []  

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

             var elt = ain  

             if (!seen[elt]) {  

                aout.push(elt)  

                seen[elt] = true  

                }  

             }  

          return aout

}

This function will remove the double entries.

In your case it would be:

tableCallouts = unique(tableCallouts);

graphicCallouts = unique(graphicCallouts);

Votes

Translate

Translate

Report

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
Advocate ,
May 22, 2018 May 22, 2018

Copy link to clipboard

Copied

I usually use this snippet to compare arrays.

function equalArrays(array1, array2) {

    if (!array1 || !array2) {

        return false;

    }

    if (array1.length !== array.length) {

        return false;

    }

    for (var i = 0, il = array1.length; i < il; i++) {

        // Check if we have nested arrays

        if (array1 instanceof Array && array2 instanceof Array) {

            // recurse into the nested arrays

            if (!array1.equalArrays(array2))

                return false;

        } else if (array1 != array2) {

            // Warning - two different object instances will never be equal: {x:20} != {x:20}

            return false;

        }

    }

    return true;

}

Votes

Translate

Translate

Report

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
Engaged ,
May 22, 2018 May 22, 2018

Copy link to clipboard

Copied

LATEST

Your snippet will only work if both arrays have the same length.

BTW: in line 6 it has to be "... !== array2.length) ..."

But if there are two arrays with different content how do you filter out the equals in both? Or the differences?

So IMO the result of a script as requested would be:

- length of 1st array (after doubles removed)

- length of 2nd array (after doubles removed)

- list of equals in both arrays

- lists of items in 1st array w/o equals mentioned above

- lists of items in 2nd array w/o equals mentioned above

Votes

Translate

Translate

Report

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