0

so today I have an Array with several strings in them, all ending with , example: jazz, latin, trance,

I need to remove the , off the last element in the Array. Searching Stackoverflow I found several answers and tried the code below, but no luck so far :(

// How I'm generating my Array (from checked checkboxes in a Modal)
    role_Actor = [];
    $('.simplemodal-data input:checked').each(function() {
        role_Actor.push($(this).val());
    });

// roleArray = my Array
var lastEl = roleArray.pop();
    lastEl.substring(0, lastEl.length - 1);
    roleArray.push($.trim(lastEl));


// My function that displays my strings on the page
    $.each(roleArray, function(index, item) {
        $('.'+rowName+' p').append(item+', ');
    });

// Example of the Array:
    Adult Animated, Behind the Scenes, Documentary,

Thanks for taking a look!


Thanks @Claire Anthony! for the fix!!!

Community
  • 1
  • 1
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
  • Why do the strings have commas at the end to begin with? You should probably fix your array generation procedure. Are you sure that the commas are actually part of the string and that the array just doesn't have an empty last element? – Felix Kling Apr 05 '13 at 14:56
  • 1
    give the example array you are trying to trim – theshadowmonkey Apr 05 '13 at 15:00
  • The strings don't have ',' with them, but I have to add them to make the displayed content look correct. I've added my function that does that above – Leon Gaban Apr 05 '13 at 15:00
  • 3
    Use `array.join(',')` instead of adding commas yourself. – Blazemonger Apr 05 '13 at 15:01
  • `Adult Animated, Behind the Scenes, Documentary,` is not an array... an actual example of the array (as code) and an explanation what you are trying to do (the bigger picture) would be really helpful. Right now, your question appears to be an XY problem. – Felix Kling Apr 05 '13 at 15:06

3 Answers3

4

You forgot to assign lastEl before putting it back in to roleArray:

lastEl = lastEl.substring(0, lastEl.length - 1);

As the comments suggest, and as you're just using this for display purposes, you should remove the commas from the elements in roleArray and use the join method, like so:

var roleArray = ['latin', 'jazz', 'trance'];
$('#example').append(roleArray.join(', '));
  • This is the closest one to my solution! But I'm getting double arrays being displayed? The last , is not showing up now tho $.each(roleArray, function(index, item) { //$('.'+rowName+' p').append(item+', '); $('.'+rowName+' p').append(roleArray.join(', ')); }); – Leon Gaban Apr 05 '13 at 15:10
  • 2
    You don't need to loop at all – billyonecan Apr 05 '13 at 15:12
1

Try this:

$.each(arr, function(i, val) {
    arr[i] = val.substring(0, val.length - 1);
});
Marcus
  • 5,407
  • 3
  • 31
  • 54
1

EDIT now that he question has been edited to reflect reality, this is no longer valid.... =)

// make an array
var a = ['one,two,three,', 'four,five,six,'];

// get the last element
var lastEl = a[a.length -1];

// knock off the last character
var trimmedLast = lastEl.substring(0, lastEl.length - 1);

alert(trimmedLast);

// as a function that will return said
// please note you should write error handling and so 
// on in here to handle empty or non-array inputs.
function lastArrayThing(myArray) {
    var lastEl = a[a.length -1];
    return lastEl.substring(0, lastEl.length - 1);
}

alert( lastArrayThing(a) );

Code in action: http://jsfiddle.net/SB25j/

veddermatic
  • 1,082
  • 7
  • 15