Highlighted

jsx array.splice broken

Contributor ,
Oct 14, 2020

Copy link to clipboard

Copied

This script should empty the array:

var myarray = ['only element'];
myarray.splice(0, 1);

However, it retains the only element... (found out in Illu 2020, both PC and Mac. I have not tried other apps or versions so far)

Other splice operations work fine (deleting last entry, replacing only entry with new values)

 

Where should I post thatto bring it to Adobe's attention

TOPICS
Bug, Scripting

Views

68

Likes

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

jsx array.splice broken

Contributor ,
Oct 14, 2020

Copy link to clipboard

Copied

This script should empty the array:

var myarray = ['only element'];
myarray.splice(0, 1);

However, it retains the only element... (found out in Illu 2020, both PC and Mac. I have not tried other apps or versions so far)

Other splice operations work fine (deleting last entry, replacing only entry with new values)

 

Where should I post thatto bring it to Adobe's attention

TOPICS
Bug, Scripting

Views

69

Likes

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
Oct 14, 2020 0
Adobe Community Professional ,
Oct 14, 2020

Copy link to clipboard

Copied

Hi,

I tried the same code as yours and it is working perfectly fine(Tested on mac). Just added alert to check length of an array and it returns 0. 

var myarray = ['only element'];
myarray.splice(0, 1);
alert(myarray.length)

  

Best regards

Likes

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
Reply
Loading...
Oct 14, 2020 1
Most Valuable Participant ,
Oct 14, 2020

Copy link to clipboard

Copied

Like @Charu Rajput wrote - it is working fine (under Windows too)

var myarray = ['only element'];
alert(myarray[0]);
myarray.splice(0, 1);
alert(myarray.length);
myarray.push('new element');
alert(myarray[0]);
alert(myarray.length);

 

Have fun

😉

 

Likes

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
Reply
Loading...
Oct 14, 2020 1
Contributor ,
Oct 14, 2020

Copy link to clipboard

Copied

well, the situation seems to be a bit  strange...

Executing

var myarray = ['only element'];
myarray.splice(0, 1);
alert(myarray.length)

in a script file works fine.

Typing the first line into ESTK javascript console shows "only element" as a result, and typing the second line does the same - but typing myarray.length in the console correctly shows zero.

I could find the wrong behaviour in a script like this

// some initializing the array
for(var n = 0 ; n < myarray.length ; )
  if(myarray[n].match(/pattern/))
    myarray.splice(n, 1)
  else
    n++

With a single entry in the array the script would just loop, Revised script works:

// some initializing the array
for(var n = 0 ; n < myarray.length ; )
  if(myarray[n].match(/pattern/))
    if(n == myarray.length-1)
      myarray.pop()
    else
      myarray.splice(n, 1)
  else
    n++

Likes

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
Reply
Loading...
Oct 14, 2020 0
Adobe Community Professional ,
Oct 14, 2020

Copy link to clipboard

Copied

As far as I understand by looking your code (not 100% sure if your complete script doing something else), you want to remove items from an array by matching the pattern. As @pixxxelschubser , suggested you have to loop in reverse order. Here is the sample script that may help you understand how to remove all items from an array that matches the pattern.

 

myarray = ['one element', 'two element', 'three'];
alert(myarray);
for(var n = myarray.length-1 ; n >=0 ; n--){
  if(myarray[n].match(/element/)){
      myarray.splice(n, 1);
  }
}
alert(myarray);

 

In the above script I am removing all items from an array that have 'element' in it.

Best regards

Likes

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
Reply
Loading...
Oct 14, 2020 1
Most Valuable Participant ,
Oct 14, 2020

Copy link to clipboard

Copied

If you want to remove items, loop backwards.

Likes

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
Reply
Loading...
Oct 14, 2020 1