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

jsx array.splice broken

Contributor ,
Oct 14, 2020 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

347

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

Community Expert , Oct 14, 2020 Oct 14, 2020

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

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 14, 2020 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

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
Community Expert ,
Oct 14, 2020 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

😉

 

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
Contributor ,
Oct 14, 2020 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++

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
Community Expert ,
Oct 14, 2020 Oct 14, 2020

Copy link to clipboard

Copied

LATEST

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

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
Community Expert ,
Oct 14, 2020 Oct 14, 2020

Copy link to clipboard

Copied

If you want to remove items, loop backwards.

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