1

I need to remove the last element comma in Javascript array

var arr =  ["AAA,","BBB,"];

I need the result below

var arr =  ["AAA,","BBB"];

Any help is appreciated...

8
  • What you have tried so far? Post your inputs Commented Mar 30, 2017 at 4:57
  • 1
    don't put it there in the first place? See this is the problem with such minimal code ... Commented Mar 30, 2017 at 4:57
  • 1
    Why do you want to leave the comma in the other element? Looks like an XY problem to me. Commented Mar 30, 2017 at 4:58
  • or a KY problem Commented Mar 30, 2017 at 4:59
  • is there a specific reason you want to do this? Commented Mar 30, 2017 at 5:06

7 Answers 7

3

var arr =  ["AAA,","BBB,"];

arr[arr.length - 1] = arr[arr.length - 1].replace(',', '');

console.log(arr);

Sign up to request clarification or add additional context in comments.

1 Comment

Note that this removes specifically the first comma in the last element, which works for the data shown where there is only one comma but might not be appropriate more generally.
1

Simply use with replace()

var arr =  ["AAA,","BBB,"];

arr[arr.length-1] = arr[arr.length-1].replace(/\,/g,"");

console.log(arr)

7 Comments

This will remove all commas from string. Not necessarily last. Ex. ['ABC,', 'X,Y,Z,'].
Using a regex is likely to be slower than a simple string in this case. @Tushar OP want to remove the comma from the last element. Not at the last index of the last element. So I think it complies.
Commas don't need to be escaped in regular expressions.
@QuentinRoy If string contains commas, not just last comma and OP want to keep it then this will not work.
@Tushar yes, but he never said the comma is always at the end nor that he wanted to keep comma not at the nor that there may be comma not at the end.
|
0

One of the other way is this:

var arr =  ["AAA,",",BBB,"];
arr.push(arr.pop().replace(/,$/, ''));
console.log(arr);

Comments

0

This answer explains how you can do it using regex:

>> var str = "BBB,"
>> str = str.replace(/,[^,]*$/ , "")
>> str
>> "BBB"

1 Comment

"This answer explains" - Well...it doesn't really explain, but it does show an example. Why have you put the [^,]* part in your regex? OP asked about removing a comma, not removing a comma and the characters that follow it.
0
var arr =  ["AAA,","BBB,"];
var lastelmnt = arr[(arr.length)-1].replace(',', '');
arr.splice(((arr.length)-1),1,lastelmnt);

Output :

["AAA,", "BBB"]

3 Comments

that does exactly null
Even aside from the fact that this doesn't modify the array elements at all, why would you loop over the whole array when the question asks about modifying only the last element?
Now I need to changed my solution. thanks for your suggestion.
0
arr[arr.length-1] = arr[arr.length-1].slice(0,-1)

3 Comments

Please add some explanation. Code-only answers are not helpful.
.slice(0,-1) is a lot more convenient than .slice(0,arr[arr.length-1].length-1).
Considering OP is newbie, I'd add some explanation to the answer. It's always good to add explanation to answer.
0

using JavaScript string split() method & Array splice() method.

DEMO

var arr =  ["AAA,","BBB,"];

var arrLastElement = arr[arr.length-1];

var splitStr =  arrLastElement.split(',');

var strWithoutComma = splitStr[0];

arr.splice(arr.length-1);
arr.push(strWithoutComma);

console.log(arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.