Ramda - 从对象和 return 数组中选取
Ramda - Pick from an object and return an array
我有一个 id 数组,我需要用它来从对象中获取扩展信息,所以它看起来像这样:
const arrayOne = [1, 2]
const objectOne =
{
"1": {
"id": 1,
"reference": "ee",
"content": {
"blocks": [
{
"type": "plain",
"data": {
"text": "Lorem2"
}
}
]
}
},
"2": {
"id": 2,
"reference": "dd",
"content": {
"blocks": [
{
"type": "plain",
"data": {
"text": "Lorem"
}
}
]
}
},
"3": {
"id": 3,
"reference": "dd",
"content": {
"blocks": [
{
"type": "plain",
"data": {
"text": "Lorem"
}
}
]
}
}
}
然后我使用 Ramda 只从这样的数组中获取 ID:
R.pick(arrayOne)(objectOne)
但后来我得到了对象,我需要的是一个数组。如何转换?
在这种情况下,您可能需要 props
:
Acts as multiple prop: array of keys in, array of values out. Preserves order.
props(['1', '2'], {1: "foo", 2: "bar"});
//=>["foo","bar"]
我有一个 id 数组,我需要用它来从对象中获取扩展信息,所以它看起来像这样:
const arrayOne = [1, 2]
const objectOne =
{
"1": {
"id": 1,
"reference": "ee",
"content": {
"blocks": [
{
"type": "plain",
"data": {
"text": "Lorem2"
}
}
]
}
},
"2": {
"id": 2,
"reference": "dd",
"content": {
"blocks": [
{
"type": "plain",
"data": {
"text": "Lorem"
}
}
]
}
},
"3": {
"id": 3,
"reference": "dd",
"content": {
"blocks": [
{
"type": "plain",
"data": {
"text": "Lorem"
}
}
]
}
}
}
然后我使用 Ramda 只从这样的数组中获取 ID:
R.pick(arrayOne)(objectOne)
但后来我得到了对象,我需要的是一个数组。如何转换?
在这种情况下,您可能需要 props
:
Acts as multiple prop: array of keys in, array of values out. Preserves order.
props(['1', '2'], {1: "foo", 2: "bar"});
//=>["foo","bar"]