rob.m rob.m - 2 years ago 104
Javascript Question

how to remove a comma from an object?

I tried:

val['*'].replace(/,/g , '')


But i still have the
comma



,Битва при реке Пэккан

,Bitka kod Baekganga

,Битва при річці Пек

,Trận Bạch Giang

,白江口之战


UPDATE

data.parse.langlinks.map(function(val){
return val['*']
});

Answer Source

From what I've finally been able to grab, you are using this kind of line (stripped a bit)

$('#results').append('<li>' + curTitle + "<br>" +   data.parse.langlinks.map(function(val){ return val['*'] + "<br>" }) + '</li>');

What happens is that the result from data.parse.langlinks.map is an array, and to concatenate that to the

  • string, its values are concatenated with the dreaded comma.

    You can see by yourself using this:

    console.log(data.parse.langlinks.map(function(val){ return val['*'] + "<br>" }).toString());
    

    To fix this, you have multiple possibilities, the best is to leverage jQuery's flexible append this way:

    $('#results').append('<li>' , curTitle , "<br>" ,   data.parse.langlinks.map(function(val){ return val['*'] + "<br>" }) , '</li>');
    

    The good thing about append is that it takes unlimited amount of parameters, just as if you called it once for every param. And it also accepts array, and handle them correctly.

  • Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download