Replace null values to empty values in a JSON OBJECT

前端 未结 8 1769
無奈伤痛
無奈伤痛 2020-12-31 06:54

Hi I\'ve got a JSON object provided by an ajax request.

Some of the values inside the json appears as null, but I want an empty String inst

8条回答
  •  半阙折子戏
    2020-12-31 07:29

    Here's how you should be doing it, replacing the objects values with empty strings, not stringifying it

    $.post("/profil_process/wall/preview-post.php",param, function (data){
    
        (function removeNull(o) {
            for(var key in o) {
                if( null === o[key] ) o[key] = '';
                if ( typeof o[key] === 'object' ) removeNull(o[key]);
            }
         })(data);
    
         $('#previewWall').html(
              getPostWall(
                  data.type,
                  data.titre,data.url,
                  data.description,
                  data.media,
                  data.photo_auteur,
                  data.nom_auteur,
                  data.url_auteur,
                  data.date_publication
              )  // ^^^ why not just pass the entire object ?
        ).fadeIn();
    
        $(".bouton-vertM").show();
        $("#wLoader").hide();
    
    },'json');
    

提交回复
热议问题