I have this hash:
{
a: [1,2],
b: [1,2,3]
}
I need to generate a string like this:
a=1&a=2&b=1&b=2&b=3
How can I solve this problem ? I'm taking a look at lodash but I cant solve it.
Thanks.
I have this hash:
{
a: [1,2],
b: [1,2,3]
}
I need to generate a string like this:
a=1&a=2&b=1&b=2&b=3
How can I solve this problem ? I'm taking a look at lodash but I cant solve it.
Thanks.
Using javascript Object.keys(), map() and join() with ES6 arrow function
var arr = {
a: [1, 2],
b: [1, 2, 3]
};
var res = Object.keys(arr).map(v => arr[v].map(v1 => v + '=' + v1).join('&')).join('&');
document.write(res);
Or without arrow function
var arr = {
a: [1, 2],
b: [1, 2, 3]
};
var res =
// get object keys
Object.keys(arr)
// iterate over object keys and iterate
.map(function(v) {
// iterate over inner array
return arr[v].map(function(v1) {
// generate prefered string and return
return v + '=' + v1
// cconcatenate string array
}).join('&')
// concatenate string array
}).join('&');
document.write(res);
function hashToString(hash) {
var str = '';
for ( prop in hash ) {
if ( !hash.hasOwnProperty(prop) ) continue;
for (var i = 0; i < hash[prop].length; i++) {
str += prop + '=' + hash[prop][i] + '&';
};
}
str = str.slice(0, str.length-1);
return str;
}
Try that out!
You do this with a for ... in loop. You should be checking hasOwnProperty so you don't go through unexpected prototype properties.
We'll do that with a regular for loop because these items are just arrays.
In here, we want to adjust our string:
str += prop + '=' + hash[prop][i] + '&';
This adds our property name (saved as prop from the for ... in loop), then the value in that property's array at the given index i. We'll leave it with a trailing & that we will remove just outside of the loop.
Use a for in loop to iterate over each property in your hash object, and from there, add each value to the string, like so:
function hashToQueryString(hash) {
var query = '';
for (key in hash) {
for (i=0; i<=key.length; i++) {
query += key+'='+ myJSON[key]+'&';
}
}
//remove the trailing &
query = query.substr(0, query.length -1);
return query;
}
(Keep in mind that you should also be checking that each property in hash contains a valid array, and that you're not accidentally enumerating any properties you don't want)
You could use qs for that. It will also handle all the encoding needed if you happen to go beyond integers.
qs.stringify({
a: [1,2],
b: [1,2,3]
}, { indices: false });
If you have to stay with poor old ES5, and don't want to use another library for a simple task:
var myHash = {
a: [1,2],
b: [1,2,3]
}
var myString = '';
Object.keys(myHash).map(function(key, index) {
myHash[key].map(function(value) {
myString += (myString ? '&' : '') + key + '=' + value;
});
});
I am sure there is a cleaner way, but here is some options with reduce and map.
var obj = {
a: [1,2],
b: [1,2,3]
};
var params = Object.keys(obj).reduce( function (arr, key){
arr.push(key + "=" + obj[key].join("&" + key + "="))
return arr;
}, []).join("&");
or if the values need to be encoded for the querystring
var params = Object.keys(obj).reduce( function (arr, key){
var mapped = obj[key].map( function (val) { return key + "=" + encodeURIComponent(val); }).join("&");
arr.push(mapped)
return arr;
}, []).join("&");
let hash = {
a: [1, 2],
b: [1, 2, 3]
};
let str = '';
for (let key in hash) {
for (let value of hash[key]) {
str += `${key}=${value}&`;
}
}
alert(str.slice(0, -1));