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?
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);
}
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) ]);
?
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) ]);
?
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.
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.
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.
Copy link to clipboard
Copied
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?