Skip to main content
smithcgl9043167
Inspiring
August 6, 2022
Answered

Help on slicing array items

  • August 6, 2022
  • 1 reply
  • 706 views

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?

 

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);
}

 

This topic has been closed for replies.
Correct answer c.pfaffenbichler

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) ]);

1 reply

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
August 7, 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) ]);

smithcgl9043167
Inspiring
August 7, 2022

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.

c.pfaffenbichler
Community Expert
Community Expert
August 7, 2022

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.