Skip to main content
Iain Robinson
Known Participant
December 22, 2015
Question

[JS] problem with removing entries from an array

  • December 22, 2015
  • 2 replies
  • 1236 views

Hi all

Using ID CS5 and I have an array of all the paragraphs in my document that I want to examine and I am having problem removing items from it. I want to remove any paras styled using particular para styles and after that I am going to use some established code to do some other stuff. The other (established) stuff works well but the code to remove certain paragraphs is not working as expected.

My code;

app.findGrepPreferences = null;

app.changeGrepPreferences = null;

app.findChangeGrepOptions.includeMasterPages = false;

app.findChangeGrepOptions.includeLockedLayersForFind = false;

app.findGrepPreferences.findWhat = "^.+$";

f = app.activeDocument.findGrep ();

//first goes through array deleting any para styled as "B Head" or "C Head"

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

  if (f.appliedParagraphStyle.name == "B Head"||f.appliedParagraphStyle.name == "C head")

  f.splice(i, 1);

}


If I create a test document with 10 or 12 paras and add in two consecutive headings the code above will only remove the first one. Non-consecutive paras are removed as expected but I don't know what I have done wrong with the loop that uses splice to remove part of it.

Can anyone advise please?

Iain

This topic has been closed for replies.

2 replies

Marc Autret
Legend
December 22, 2015

Hi Iain,

When you use myArray.splice(i,1) in a loop you must iterate from the end of the array, otherwise the index becomes wrong as soon as an element is removed.

So:

for( i = myArray.length ; i-- ; ){ if( someCondition ) myArray.splice(i,1); }

@+

Marc

Iain Robinson
Known Participant
December 23, 2015

Thanks guys.

Kai - I want to remove certain paragraphs (with particular paragraph styles) from the array. This is the first of a two stage process. I could certainly play with gathering the array using your method but I do want to delete things.

Marc - I always forget about working from the end, probably because I never fully got my head round it. I have amended the code but now it doesn't appear to do anything. If I use alert to display the length of the array before and after the for loop I get the same value, so the if statement doesn't appear to be doing anything any more.

app.findGrepPreferences = null;

app.changeGrepPreferences = null;

app.findChangeGrepOptions.includeMasterPages = false;

app.findChangeGrepOptions.includeLockedLayersForFind = false;

app.findGrepPreferences.findWhat = "^.+$";

f = app.activeDocument.findGrep ();

alert(f.length);

//first goes through array deleting any para styled as "B Head" or "C Head"

for (i = f.length; i =0; i--) {

  if (f.appliedParagraphStyle.name == "B Head"||f.appliedParagraphStyle.name == "C head")

  f.splice(i, 1);

}

alert(f.length);

What am I missing?

Iain Robinson
Known Participant
December 23, 2015

In the for loop, if I say i>=0 (rather than i=0) then the if condition creates an "undefined is not an object" error.

Kai Rübsamen
Participating Frequently
December 22, 2015

Iain,

if you do it like your way, I would assume, that the references are messed up. Maybe you could loop backwards, but I would do it in another way:

Why collect all paragraphs with find/change instead of 'app.documents[0].stories.everyItem().paragraphs.everyItem().getElements();'  ?

Then loop through the paras and execute your other stuff inside the if-clause

No need to remove something here?!

Kai