关于如何使用reduce
重构我的功能的任何建议,因为我不应该在这里使用map
?
答案 0 :(得分:3)
试试这个:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="22.5778mm" height="22.5778mm"
viewBox="0 0 64 64"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.2" baseProfile="tiny">
<title>SVG Vector primitive</title>
<desc>An SVG drawing created by the QSvgGenerator </desc>
<defs>
</defs>
<g fill="#000000" fill-opacity="1" stroke="none" transform="matrix(20,0,0,20,32,32)"
font-family="Helvetica" font-size="12" font-weight="400" font-style="normal" >
<g stroke="black" stroke-width="0.2" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel" >
<path vector-effect="non-scaling-stroke" fill-rule="evenodd" d="M-0.224514,-0.309017 L-0.951057,-0.309017 L-0.363271,0.118034 L-0.587785,0.809017 L0,0.381966 L0.587785,0.809017 L0.363271,0.118034 L0.951057,-0.309017 L0.224514,-0.309017 L0,-1 L-0.224514,-0.309017"/>
</g>
</g>
</svg>
或没有模板字符串:
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('&');
}
答案 1 :(得分:2)
此处使用map
代替reduce
是合适的,您只应映射到concat
之前join
可以function serializeParams(obj) {
return Array.protoype.concat.apply([], keys(obj).map(key => {
let value = obj[key];
return Array.isArray(value)
? value.map(v => key +"=" + encodeURIComponent(v))
: [key + "=" + encodeURIComponent(value)];
})).join("&");
}
的数组数组。
session.connect()