javascript - Using reduce fnt instead of map -
any advise on how can refactor function using reduce
, should not use map
here?
try this:
function serializeparams (object) { return object.keys(object) .reduce((query, key) => array.isarray(object[key]) ? query.concat(object[key].map(value => `${key}=${encodeuricomponent(value)}`)) : query.concat(`${key}=${encodeuricomponent(object[key])}`) , []) .join('&'); }
or without template strings:
function serializeparams (object) { return object.keys(object) .reduce((query, key) => array.isarray(object[key]) ? query.concat(object[key].map(value => key + '=' + encodeuricomponent(value))) : query.concat(key + '=' + encodeuricomponent(object[key])) , []) .join('&'); }
Comments
Post a Comment