[英]How can I deeply map over object with Ramda
I'm trying to find all "template values" eg { template: 'Date: <now>'}
using a map function to get this basic behaviour: 我正在尝试使用地图函数查找所有“模板值”,例如
{ template: 'Date: <now>'}
以获取此基本行为:
deepMap(mapFn, {a: 1, b: { c: 2, d: { template: 'Date: <now>'}}})
>> {a: 1, b: { c: 2, d: 'Date: 13423234232'}}
This is what I have so far. 到目前为止,这就是我所拥有的。 The interpolation of the template object does happen, but it does not replace the value.
确实发生了模板对象的插值,但不会替换该值。
const obj = {a: 1, b: { c: 2, d: { template: 'Date: <now>'}}};
const deepMap = (fn, xs) =>
mapObjIndexed(
(val, key, obj) =>
or(is(Array, val), is(Object, val))
? deepMap(fn, fn(val))
: fn(val),
xs
);
const checkFn = ({ template }) => template;
const transformFn = (val, key) => {
const interpolated = val.template.replace('<now>', Date.now())
console.log(interpolated);
return interpolated;
};
const mapFn = n =>
checkFn(n)
? transformFn(n)
: n;
console.clear();
deepMap(mapFn, obj);
>> {"a": 1, "b": {"c": 2, "d": {}}}
The problem is you are calling deepMap
on the mapped value again - but the mapped value isn't an object anymore, but a string. 问题是您再次在映射值上调用
deepMap
但是映射值不再是一个对象,而是一个字符串。
or(is(Array, val), is(Object, val))
? deepMap(fn, fn(val))
: fn(val),
In case val is { template: 'Date: <now>'}
, val is an object and could be deep-mapped, but fn(val)
is a String ( "Date: 123123123"
) which should simply be returned. 在val是
{ template: 'Date: <now>'}
,val是一个对象,可以进行深层映射,但是fn(val)
是一个字符串( "Date: 123123123"
),应该简单地返回它。 One solution is to make the is
checks on the mapped value, not the original value: 一个解决方案是使
is
上的映射值,而不是原始值检查:
(val, key) => {
const mappedVal = fn(val);
return or(is(Array, mappedVal), is(Object, mappedVal))
? deepMap(fn, mappedVal)
: mappedVal;
},
Another possibility would be to check whether the map-function returned something else than the original value and don't recurse in this case. 另一种可能性是检查map函数是否返回了原始值以外的其他值,并且在这种情况下不进行递归。
Something like this should work: 这样的事情应该起作用:
const {map, has, is} = R const transformTemplate = ({template}) => template.replace('<now>', Date.now()) const deepMap = (xs) => map(x => has('template', x) ? transformTemplate(x) : is(Object, x) || is(Array, x) ? deepMap(x) : x, xs) const result = deepMap({a: 1, b: { c: 2, d: { template: 'Date: <now>'}}}) // => {a: 1, b: {c: 2, d: "Date: 1542046789004"}} console.log(result)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
If you wanted to pass in the transformation function, you can change it slightly to 如果要传递转换函数,可以对其稍作更改以
const deepMap = (transformer, xs) => map(x => has('template', x)
? transformer(x)
: is(Object, x) || is(Array, x)
? deepMap(transformer, x)
: x, xs)
const result = deepMap(transformTemplate, {a: 1, b: { c: 2, d: { template: 'Date: <now>'}}})
And of course you can wrap that in curry
if you like. 当然,您也可以根据需要将其包装在
curry
。
I don't have time right now to investigate why this approach, which looks right at first glance, doesn't work. 我现在没有时间研究为什么乍看之下这种方法行不通。 I'm hoping it's something simple:
我希望这很简单:
const deepMap = map(cond([
[has('template'), transformTemplate],
[is(Object), deepMap],
[is(Array), deepMap],
[T, identity]
]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.