2011-08-12 29 views

cevap

17

bir js sözlüğe dışına

?key=value?key=value 

gibi bir şey almak istiyorum, sen jQuery.param kullanabilirsiniz:

var params = { width:1680, height:1050 }; 
var str = jQuery.param(params); 
// str is now 'width=1680&height=1050' 

Aksi halde, burada bunu yapan bir işlev:

function serialize(obj) { 
    var str = []; 
    for(var p in obj) 
    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); 
    return str.join("&"); 
} 
alert(serialize({test: 12, foo: "bar"})); 
+1

Kullanımı:?' Var str = $ .param (params); 'yerine şimdi. – danger89

4

ECMAScript'te 2016 yılında aynı:

let params = { width:1680, height:1050 }; 
// convert object to list -- to enable .map 
let data = Object.entries(params); 
// encode every parameter (unpack list into 2 variables) 
data = data.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`); 
// combine into string 
let query = data.join('&'); 
console.log(query); // => width=1680&height=1050 

Veya, tek astar olarak:

let params = { width:1680, height:1050 }; 
Object.entries(params).map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('&'); 
// => "width=1680&height=1050" 
İlgili konular