Ramda - 如何合并 2 个或多个对象数组
Ramda - how to merge 2 or more arrays of objects
我正在尝试使用 Ramda 将对象数组合并为一个干净的数组,但我需要一些帮助。我有下面的示例 JSON。在此示例中,我有 2 个组,但组数可以是 3、4、10。我对每个组的 tableItems
数组感兴趣。
const groups = [
{
id: '',
name: '',
tableItems: [
{
id: 1,
name: 'John'
},
{
id: 2,
name: 'Paul'
},
{
id: 3,
name: 'Mary'
}
]
},
{
id: '',
name: '',
tableItems: [
{
id: 10,
name: 'Brian'
},
{
id: 20,
name: 'Joseph'
},
{
id: 30,
name: 'Luke'
}
]
}
];
我试过这样的事情:
let mapValues = x => x.tableItems;
const testItems = R.pipe(
R.map(mapValues)
)
然后我得到了我的 tableItems
数组,现在我想将它们合并到一个数组中。
[
[
{
"id": 1,
"name": "John"
},
{
"id": 2,
"name": "Paul"
},
{
"id": 3,
"name": "Mary"
}
],
[
{
"id": 10,
"name": "Brian"
},
{
"id": 20,
"name": "Joseph"
},
{
"id": 30,
"name": "Luke"
}
]
]
如有任何帮助,我们将不胜感激。
用R.chain贴图压平,用R.prop得到tableItems
:
const fn = R.chain(R.prop('tableItems'));
const groups = [{"id":"","name":"","tableItems":[{"id":1,"name":"John"},{"id":2,"name":"Paul"},{"id":3,"name":"Mary"}]},{"id":"","name":"","tableItems":[{"id":10,"name":"Brian"},{"id":20,"name":"Joseph"},{"id":30,"name":"Luke"}]}];
const result = fn(groups);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
我正在尝试使用 Ramda 将对象数组合并为一个干净的数组,但我需要一些帮助。我有下面的示例 JSON。在此示例中,我有 2 个组,但组数可以是 3、4、10。我对每个组的 tableItems
数组感兴趣。
const groups = [
{
id: '',
name: '',
tableItems: [
{
id: 1,
name: 'John'
},
{
id: 2,
name: 'Paul'
},
{
id: 3,
name: 'Mary'
}
]
},
{
id: '',
name: '',
tableItems: [
{
id: 10,
name: 'Brian'
},
{
id: 20,
name: 'Joseph'
},
{
id: 30,
name: 'Luke'
}
]
}
];
我试过这样的事情:
let mapValues = x => x.tableItems;
const testItems = R.pipe(
R.map(mapValues)
)
然后我得到了我的 tableItems
数组,现在我想将它们合并到一个数组中。
[
[
{
"id": 1,
"name": "John"
},
{
"id": 2,
"name": "Paul"
},
{
"id": 3,
"name": "Mary"
}
],
[
{
"id": 10,
"name": "Brian"
},
{
"id": 20,
"name": "Joseph"
},
{
"id": 30,
"name": "Luke"
}
]
]
如有任何帮助,我们将不胜感激。
用R.chain贴图压平,用R.prop得到tableItems
:
const fn = R.chain(R.prop('tableItems'));
const groups = [{"id":"","name":"","tableItems":[{"id":1,"name":"John"},{"id":2,"name":"Paul"},{"id":3,"name":"Mary"}]},{"id":"","name":"","tableItems":[{"id":10,"name":"Brian"},{"id":20,"name":"Joseph"},{"id":30,"name":"Luke"}]}];
const result = fn(groups);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>