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

Help on slicing array items

Engaged ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

Guys, are you all right?
Guys, I'm new to scripts and I'm trying to store some values in series within each item of an array, however, I get an error when I use the "slice()" method to separate each value.
What am I doing wrong? Can anyone point me in some direction where I can solve this problem?

Untitled-1.png

 

var arr_maisServicos =[];

var quantidd = 1;
var pefilIDfx = "Pefil xxxxx 000";
var tamanho = "20x30";
var valorSer = 10.00;
var sep = ":";

// COLETA TODOS VOLORES AO CLICAR EM MAIS
arr_maisServicos.push([ Number(quantidd) + sep +  tamanho + sep +   pefilIDfx + sep +  Number(valorSer) ]);
arr_maisServicos.push([ Number(quantidd) + sep +  tamanho + sep +   pefilIDfx + sep +  Number(valorSer) ]);
arr_maisServicos.push([ Number(quantidd) + sep +  tamanho + sep +   pefilIDfx + sep +  Number(valorSer) ]);

adiconar()
function adiconar(){
    for (i = 0; i < arr_maisServicos.length; i++) {   
        servs = arr_maisServicos[i];
        serv1 = servs.split(":")[0];
        serv2 = servs.split(":")[1];
        serv3 = servs.split(":")[2];
        serv4 = servs.split(":")[3];
        }
    
      alert (serv1);
      alert (serv2);
      alert (serv3);
      alert (serv4);
}

 

TOPICS
Actions and scripting

Views

337

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 , Aug 07, 2022 Aug 07, 2022

servs is an Array, so »split« does not apply, even though it contains only one String

Why did you add the squared brackets in the line

arr_maisServicos.push([ Number(quantidd) + sep +  tamanho + sep +   pefilIDfx + sep +  Number(valorSer) ]);

Votes

Translate

Translate
Adobe
Community Expert ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

servs is an Array, so »split« does not apply, even though it contains only one String

Why did you add the squared brackets in the line

arr_maisServicos.push([ Number(quantidd) + sep +  tamanho + sep +   pefilIDfx + sep +  Number(valorSer) ]);

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 ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

Hi @c.pfaffenbichler , thanks for replying. As I mentioned, I'm new to scripts, I started exploring arrays that will serve me a lot.
Square brackets on the line you refer to in your question are the square brackets? push( [xxxx ] ).

arr_maisServicos.push([ Number(quantidd) + sep + size + sep + profileIDfx + sep + Number(ValueSer) ]);


I don't know where I got this from, but I decided to remove "[ ]" and it worked perfectly.
it was like this:

arr_maisServicos.push( Number(quantidd) + sep + size + sep + profileIDfx + sep + Number(ValueSer));


Thank you, your question was the solution to the problem.

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 ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

The method »split« is for Strings, Arrays are already collections of separate »objects« (edit: like Strings, Integers, Booleans, Arrays, …). 

 

I was talking about the square brackets (»[« and »]«) by which you needlessly wrapped a String into an Array which you then pushed into another 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
Engaged ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

You're absolutely right. I edited what I wrote earlier. After I removed [] it worked correctly. Thanks for pointing me in the right direction.

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 ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

LATEST

The other option would be not compacting the values into one String but adding an Array to the Array. 

So

arr_maisServicos.push([ Number(quantidd),  tamanho,   pefilIDfx,  Number(valorSer) ]);
arr_maisServicos.push([ Number(quantidd),  tamanho,   pefilIDfx,  Number(valorSer) ]);
arr_maisServicos.push([ Number(quantidd),  tamanho,   pefilIDfx,  Number(valorSer) ]);

instead of 

arr_maisServicos.push([ Number(quantidd) + sep +  tamanho + sep +   pefilIDfx + sep +  Number(valorSer) ]);
arr_maisServicos.push([ Number(quantidd) + sep +  tamanho + sep +   pefilIDfx + sep +  Number(valorSer) ]);
arr_maisServicos.push([ Number(quantidd) + sep +  tamanho + sep +   pefilIDfx + sep +  Number(valorSer) ]);

Then you would not need to split the String but could address the items directly. 

 

Though why did you add that three times to the 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