使用 reduce fnt 而不是 map
Using reduce fnt instead of map
关于如何使用reduce
重构函数的任何建议,因为我不应该在这里使用map
?
试试这个:
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('&');
}
或者没有模板字符串:
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('&');
}
在这里使用 map
而不是 reduce
是合适的,您只应该映射到可以在join
ing 之前concat
的数组数组。
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("&");
}
由于您使用 Ramda 标记了此内容,因此您可以使用concat
撰写map
或立即使用chain
。
相关文章:
- 节点中的Map Reduce
- Map/Reduce给我NaN或错误的结果(由于null)
- MongoDB map/reduce,forEach不是一个函数
- 使用map/reduce计算总数
- Javascript作用域规则和mongo-map/reduce函数
- 有MongoDB的参考吗's map reduce javascript
- Javascript如何使用高阶函数和Map/Reduce函数来汇总多个数组
- MongoDB使用Map Reduce插入来自其他集合的嵌入式文档
- 聚合来自mongodb,map reduce或任何其他方式的数据
- map(), reduce() and filter vs forEach()
- 为什么这个map reduce Promises数组不起作用,而只是减少它起作用
- 使用Map Reduce进行数据透视时出现问题
- Map Reduce for MongoDB
- 可以'我不明白为什么/如何使用map reduce
- 通过聚合框架或map/reduce将事件数据嵌入MongoDB中的文档
- MongoDB map reduce使用嵌套对象
- MongoDB Map Reduce显示"[object object]"作为键,尽管对它们进行类型
- Riak Map Reduce在JS中返回有限的数据
- 在CouchDB中使用Map-Reduce方法链接文档
- 处理CouchDB Map/Reduce JavaScript函数上的Eclipse错误的最佳方法是什么?