Using .length-1

Hi - I am wondering if anyone would be able to help me with this script. I am new to Javascript and can’t get this to work. What I am trying to do is return the pptCount except for ‘All’ ( the first 5 counts only).

I have a data set as such -
Age pptCount

30 0
30-39 30
40-49 330
50-59 1,024
60+ 839
All 2,223

My script is -

results.each(function(index) {
var ageCnt =  record.tables["BalByAge"][index].fields["pls_ageCnt"];

var field, result = "";

for (var i = 0; i < results.length -1; i++)	

	field = ageCnt;
	if (field != "") result += field;

Any help would be greatly appreciated.
Thank you

Remove the last element of the array and iterate like you’re doing.

You can remove without modifying the original array with slice(-1, 1), you can modify the original array by using splice(-1, 1), or you modify the original array and return the removed item with pop().

Let me know if that’s not an option for you.

So are you saying one option is like this? If so, that returns only the first result. I need all but the last one.

var ageCnt = record.tables[“BalByAge”][index].fields[“pls_ageCnt”].slice(-1,1);

Sorry if I misunderstood you.

You have to slice your array, I believe you can do that:

results.slice(-1, 1).each(function(index) {

If that doesn’t work it would be helpful if you can get us a link to run your code, like codepen.

I tried your suggestion with splice and slice and it still didn’t work - ugh… I am trying to create a link in code pen to send.

Thank you

say you have this arr arr = [1,2,3,4,5]

arr.length is basically just a number 5

but the index of the last number is 4
arr[4] returns 5

if i < 5 is the limit, i will not go any higher than arr[4]
if i < 5 - 1 is the limit, i will not go higher than arr[3]

Thank you for the explanation! I think I understand how it is supposed to work, I am having trouble getting the syntax right.