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

reduce options in next array

Participant ,
Jun 27, 2020 Jun 27, 2020

Copy link to clipboard

Copied

how do remove an option from an array dynamically? If I had something like the following I would want it so that selecting an option in the first dropdown would remove it from dropdowns 2 and 3 and then anything selected in dropdown 2 would be removed from dropdown 3

var win = new Window("dialog","options",undefined,{closeButton: true});
	win.orientation='column'; 

    var group1 = win.add("group");
	group1.orientation='column';

	var dropdownlist1_array = ["Select","option 1","option 2","option 3"];
	var dropdownlist2_array = ["Select","option 1","option 2","option 3"];
	var dropdownlist3_array = ["Select","option 1","option 2","option 3"];  

    var dropdownlist1 = group1.add("dropdownlist", undefined, dropdownlist1_array);   
    dropdownlist1.selection = 0;   
    dropdownlist1.preferredSize.height = 20;  
    dropdownlist1.onChange = function(){onChangeDropdownlist1();};  

    var dropdownlist2 = group1.add("dropdownlist", undefined, dropdownlist2_array);   
    dropdownlist2.selection = 0;   
    dropdownlist2.preferredSize.height = 20;
	dropdownlist2.onChange = function(){onChangeDropdownlist2();};

	var dropdownlist3 = group1.add("dropdownlist", undefined, dropdownlist3_array);   
    dropdownlist3.selection = 0;   
    dropdownlist3.preferredSize.height = 20;		

win.show();

function onChangeDropdownlist1()  
	{};

function onChangeDropdownlist2()  
	{};
TOPICS
Actions and scripting

Views

307

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

Participant , Jun 27, 2020 Jun 27, 2020

This works

 

var win = new Window("dialog","options",undefined,{closeButton: true});
	win.orientation='column'; 

    var group1 = win.add("group");
	group1.orientation='column';

	var dropdownlist1_array = ["Select","option1","option2","option3"];
	var dropdownlist2_array = ["Select","option1","option2","option3"];
	var dropdownlist3_array = ["Select","option1","option2","option3"];  

    var dropdownlist1 = group1.add("dropdownlist", undefined, dropdownlist1_array);   
    dropdownlist1.selecti
...

Votes

Translate

Translate
Adobe
Community Expert ,
Jun 27, 2020 Jun 27, 2020

Copy link to clipboard

Copied

In this case splice method would serve you. See the documentation below

Array.prototype.splice()

 

So the idea is that in the onChange method you will find the index of the element that is selected and get its index. You can get the selected item using the selection property. Once you get the index of the selected item, you can call the splice method on the array from which you want to remove the element

 

-Manan

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
Participant ,
Jun 27, 2020 Jun 27, 2020

Copy link to clipboard

Copied

When I run the following code the alert is "Selection 1" which is what I would have expected but from that I need to get "Select, Selection 2, Selection 3"

 

 

 

	var remove = dropdownlist2_array.splice (1,1);
	alert(remove);

 

 

 

 

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 ,
Jun 27, 2020 Jun 27, 2020

Copy link to clipboard

Copied

The return value is the value that you deleted, check the element in dropdownlist2_array it should have the value removed

 

-Manan

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 ,
Jun 27, 2020 Jun 27, 2020

Copy link to clipboard

Copied

Maybe the splice method will work, but I've found that I've had to remove all the items from a dropdownlist and repopulate it with the edited array.

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 ,
Jun 27, 2020 Jun 27, 2020

Copy link to clipboard

Copied

Worth a try, good point

 

-Manan

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
Participant ,
Jun 27, 2020 Jun 27, 2020

Copy link to clipboard

Copied

Chuck I am sure that I saw an answer by you along those lines but struggling to find it again

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
Participant ,
Jun 27, 2020 Jun 27, 2020

Copy link to clipboard

Copied

because you can not use indexOf i thought about something like this but although to the eye the data looks the same I am not getting a match alert

function onChangeDropdownlist1()  
	{
	var i = 0;
	while (i < dropdownlist2_array.length) {
    if (dropdownlist1_array[i] === dropdownlist1.selection) {
		alert ("match");
    var remove = dropdownlist2_array.splice(i, 1);
    } else {
      ++i;
    }
	}	  
	alert(dropdownlist2_array);
	};

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
Participant ,
Jun 27, 2020 Jun 27, 2020

Copy link to clipboard

Copied

LATEST

This works

 

var win = new Window("dialog","options",undefined,{closeButton: true});
	win.orientation='column'; 

    var group1 = win.add("group");
	group1.orientation='column';

	var dropdownlist1_array = ["Select","option1","option2","option3"];
	var dropdownlist2_array = ["Select","option1","option2","option3"];
	var dropdownlist3_array = ["Select","option1","option2","option3"];  

    var dropdownlist1 = group1.add("dropdownlist", undefined, dropdownlist1_array);   
    dropdownlist1.selection = 0;   
    dropdownlist1.preferredSize.height = 20;  
    dropdownlist1.onChange = function(){onChangeDropdownlist1();};  

    var dropdownlist2 = group1.add("dropdownlist", undefined, dropdownlist2_array);   
    dropdownlist2.selection = 0;   
    dropdownlist2.preferredSize.height = 20;
	dropdownlist2.onChange = function(){onChangeDropdownlist2();};

	var dropdownlist3 = group1.add("dropdownlist", undefined, dropdownlist3_array);   
    dropdownlist3.selection = 0;   
    dropdownlist3.preferredSize.height = 20;		

win.show();

function onChangeDropdownlist1()  
	{

	var i = 0;
	while (i < dropdownlist2_array.length) {
    if (dropdownlist1_array[i] === dropdownlist1.selection.text) {
    var remove = dropdownlist2_array.splice(i, 1);
	var remove = dropdownlist3_array.splice(i, 1);
    } 
    ++i;
	}	  
   dropdownlist2.removeAll();
   for(var i=0;i<dropdownlist2_array.length;i++){
    dropdownlist2.add('item',dropdownlist2_array[i])
    }
   dropdownlist2.selection = 0;
	};

function onChangeDropdownlist2()  
	{
	var i = 0;
	while (i < dropdownlist3_array.length) {
    if (dropdownlist3_array[i] === dropdownlist2.selection.text && dropdownlist2.selection.text !== "Select") {
	var remove = dropdownlist3_array.splice(i, 1);
    } 
    ++i;
	}		
	dropdownlist3.removeAll();
   for(var i=0;i<dropdownlist3_array.length;i++){
    dropdownlist3.add('item',dropdownlist3_array[i])
    }
   dropdownlist3.selection = 0;	
	};

 thanks to the pointer from Chuck 

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