我循环遍历嵌套对象。返回数据由两个数组包装。我明白为什么会这样,但我不明白如何获得所需的数据。
const data = {
"foo": {
"bar": {
"id": "1",
"step": [{
"id": "33",
"copy": [{
"id": "1",
"text": "hello",
},
{
"id": "2",
"text": "whirl",
},
{
"id": "3",
"text": "whoa",
}
],
}]
}
}
}
pipe(
path(['foo', 'bar', 'step']),
map(step =>
step.copy.map(s => ({text: s.text}))
)
) (data)
返回数据返回:
[[{"text": "hello"}, {"text": "whirl"}, {"text": "whoa"}]]
我想把它还给我:
[{"text": "hello"}, {"text": "whirl"}, {"text": "whoa"}]
问题是step
本身就是一个array
。你需要得到它的第一个项目或在参数中传递0
。这是从路径中查找数据的纯js版本。我用过reduce()
const data = { "foo": { "bar": { "id": "1", "step": [{ "id": "33", "copy": [{ "id": "1", "text": "hello" }, { "id": "2", "text": "whirl" }, { "id": "3", "text": "whoa" } ] }] } } }
function getFromPath(obj, path) {
return path.reduce((ac, a) => ac[a] || {}, obj);
}
let res = getFromPath(data, ["foo","bar","step",0,"copy"]).map(({text}) => ({text}))
console.log(res)
使用Ramda.js实现你所需要的一个解决方案是在R.flatten之后管道R.map()
。
const data = {
"foo": {
"bar": {
"id": "1",
"step": [{
"id": "33",
"copy": [
{"id": "1", "text": "hello"},
{"id": "2", "text": "whirl"},
{"id": "3", "text": "whoa"}
],
}]
}
}
}
let res = R.pipe(
R.path(['foo', 'bar', 'step']),
R.map(step => step.copy.map(s => ({text: s.text}))),
R.flatten
) (data)
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
替代方案,另一种解决方案可能是用qazxsw poi取代外部qazxsw poi:
R.map()
R.reduce()
const data = {
"foo": {
"bar": {
"id": "1",
"step": [{
"id": "33",
"copy": [
{"id": "1", "text": "hello"},
{"id": "2", "text": "whirl"},
{"id": "3", "text": "whoa"}
],
}]
}
}
}
let res = R.pipe(
R.path(['foo', 'bar', 'step']),
R.reduce((acc, curr) => acc.concat(curr.copy.map(({text}) => ({text}))), [])
) (data)
console.log(res);
我在你的代码中看到的最简单的解决方法是用.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
替换外部的<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
。您可以选择使用map
或更好的chain
,但flatten
可以被认为(至少当应用于数组;它实际上更一般),因为unnest
后面是chain
。
这会做你想要的。但是我建议如果你要为其余部分使用无点的Ramda代码,你也可以替换你的内部lambdas来得到这样的东西:
map
unnest
这对我来说很好看。但请注意,解构使得原始JS比以前更加清晰。这应该也有效:
const transform = pipe(
path(['foo', 'bar', 'step']),
chain(prop('copy')),
project(['text'])
)
const data = {"foo": {"bar": {"id": "1", "step": [{"copy": [{"id": "1", "text": "hello"}, {"id": "2", "text": "whirl"}, {"id": "3", "text": "whoa"}], "id": "33"}]}}}
console.log(transform(data))
在这种情况下,Ramda版本对我来说更容易阅读,但只有很小的区别。
由于copy包含一个数组,因此您需要指定我们将循环遍历该数组。你有正确的想法,然后改变价值。
这个
<script src="//bundle.run/[email protected]"></script>
<script>
const {pipe, path, chain, prop, project} = ramda
</script>
需要是
const transform = ({foo: {bar: {step}}}) => step.flatMap(
({copy}) => copy.map(({text}) => ({text}))
)
const data = {"foo": {"bar": {"id": "1", "step": [{"copy": [{"id": "1", "text": "hello"}, {"id": "2", "text": "whirl"}, {"id": "3", "text": "whoa"}], "id": "33"}]}}}
console.log(transform(data))
或者step.copy.map(s => ({text: s.text}))
data['foo']['bar']['step'][0]['copy'].map(s => ({text: s.text}))
例
data.foo.bar.step[0].copy
然后你可以管道
data.foo.bar.step.map(v => {
return v.copy.map(v => {
return { text: v.text}
})
});
获得数组或数组的原因是因为可以迭代const data = {
"foo": {
"bar": {
"id": "1",
"step": [{
"id": "33",
"copy": [{
"id": "1",
"text": "hello",
},
{
"id": "2",
"text": "whirl",
},
{
"id": "3",
"text": "whoa",
}
],
}]
}
}
}
// Output: [{"text": "hello"}, {"text": "whirl"}, {"text": "whoa"}]
let sorted = data['foo']['bar']['step'][0]['copy'].map(s => ({text: s.text}))
// let sorted = data.foo.bar.step[0].copy.map(s => ({text: s.text}))
console.log(sorted);
值。删除2层数组意味着您只限制pipe(sorted)
数组的单个值:
step
要么
您希望将所有step
值连接到单个外部数组。
pipe(
path(['foo', 'bar', 'step', '0', 'copy']),
map(s => ({text: s.text}))
) (data)