javascript - Jquery- How to remove comma after a specific record in an array -


I am using the datasets mRender function so that a list of objects in the column is generated. I wanted to remove a specific record from the list and the record is located at the end of the list of records in an array. Something like this:

  lower, SAAS, REST, API  

I want to delete the last record (API, which I did), however, When I delete it, at the end it sticks to a comma, which results in something like this:

  lower, mother-in-law, rest, less, mother-in-law, mother-in-law,  

Assuming that all records of time after commas are all . How can I delete the record and paste it afterwards? EDIT: In this way I have to pass the blank string and remove the code and IM from the API . Please suggest this can be improved.

  mRender: function (obj) {_.each (obj, task (item, index) {var prodVal = ""; else if (item = == 'API') {prodVal = ';} ReturnVal.push ("& lt; span class =' ​​product '>" + prodVal + "");}); Return return Val.join (','); }  

Any ideas ???

Thank you!

delete the last element

Depending on the fact that you have an array Working with objects, or strings, there are a few different ways to handle it. With your example, this entry looks like your setting to evacuate instead of deleting, this is the reason why you use an extra comma after using returnVal.join (',') Getting.

Remove the last element from the array

  var mRecord = ["low", "SAAS", "REST", "API"]; MRecord.pop (); Console.log (mRecord); // array ([0] = & gt; "less", [1] = & gt; "SAAS", [2] = & gt; "REST") console.log (mRecord.join ()); Remove the last element from the string "short, SAAS, REST"  

simple underlying string

  // - will perform better in your example mRecord = MRecord.substring (0, str.lastIndexOf (",") - 1); // with regex - the bigger wire can perform better with mRecord = mRecord.replace (/, [^,] + $ /, "");  

Remove the last member of an object

When I am unsure as to the structure of the object on which you are working, Delete Emarkord [subscriber] .

Currently, the entry in the prodVal = '' array, which causes returnVal.join (',') one additional ', If you have and in your code but the initial if is not a declaration, I suspect that if you have the main value, between the last value, and the empty one

If you push and return within the conditional, then it can work with your work.

  mRender: function (obj) {_.each (obj, function (item, index) { Var prodVal = ""; if (item === 'API') {prodVal = ''; // edit Do your best bet: instead of trying to set the item to work on // generate a new array or object here.} And {returnVal.push ("& lt; span class = 'product '& Gt; "+ prodVal +" & lt; / span & gt; ");}}); Return return Val.join (','); }  

Comments