0

I want to find if the next value in a object exists or not, if it exists, add '&' else add nothing .

serializeForQueryString: function(obj) {
    var queryString ='';

    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            queryString=queryString+key+'='+ obj[key];

            if (obj.hasOwnProperty(key)!=null){
                queryString=queryString+'&';
            }
            alert(queryString);
        }
    }

    //document.write(obj.name);
    var queryString = '';

    //jquery
    //$.each(obj, function(key, value) {});

    //$.isArray

    return queryString;
},
4

1 回答 1

2

只需构建一个数组,然后使用 & 加入它:

for (var key in obj) {
            if (obj.hasOwnProperty(key)) {

                 queryString.push(key + '=' + obj[key]);


            }
 }

queryString.join('&');
于 2013-05-01T03:48:14.297 回答