JSON对象排序并生成URL参数

1、for in

function jsontourl(param) {
   let params = {}, data=[];
   let arr = Object.keys(param).sort();
   for (let i = 0; i < arr.length; i++) {
     let key = ;
     params[arr[i]] = param[arr[i]];
   }
   for (let key in params) {
     data.push(encodeURIComponent(key) + '=' + encodeURIComponent(params[key]));
   }
   data = data.join('&'); 
}

2、map

function jsontourl (params) {
    return Object.keys(params).sort().map(key => {
      return `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`
    }).join('&')
  }

 

posted @ 2017-06-22 16:56  yudis  阅读(831)  评论(0编辑  收藏  举报