如何使用ramda.js让这段代码可读性最高?
发布于 5 年前 作者 guokangf 5115 次预览 最后一次回复是 5 年前 来自 问答
const isObject = (obj) => Object.prototype.toString.call(obj) === '[object Object]';
function toArray1(children) {
const ret = children.reduce(
(acc, cur) => {
if (cur === null) return acc;
if (Array.isArray(cur)) {
acc = acc.concat(toArray1(cur));
} else if (isObject(cur) && cur.props) {
acc = acc.concat(toArray1(cur.props.children));
} else {
acc.push(cur);
}
return acc;
},
[]
);
return ret;
}
const children = [
1,
null,
[2, 3],
{
props: {
children: [4, 5]
}
}
];
const ret = toArray1(children);
console.log(ret);
7 回复
不可读
挺简单的逻辑为啥一定要用这些函数…
来个不严谨的写法🙈
@abiuDoIT 这解法 🐂 🐄 🐮
难道不是一句话的事吗?
@Shonke
?.现在js 支持这写法了? 😳@abiuDoIT 支持了 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/可选链
@Shonke node的最新版本吗?