如何使用 ramda 映射功能添加动态密钥?
How to add dynamic key using ramda map function?
我有一个这样的数组:
array = ['2020-06-03', '2020-06-05', '2020-06-06']
我想把它塑造成这样:
Object {
"2020-06-03": Object {
"selected": true,
"selectedColor": "#ff5555",
},
"2020-06-04": Object {
"selected": true,
"selectedColor": "#ff5555",
},
"2020-06-05": Object {
"selected": true,
"selectedColor": "#ff5555",
},
}
我就是这样做的:
const historyDates = map(date => {
return { selected: true, selectedColor: "#ff5555"};
}, array);
它不会工作,因为钥匙在哪里?如何使用 Ramda 表达式添加它?
我还返回了一个地图,一个新数组而不是一个充满对象的对象。
如有任何帮助,我们将不胜感激。
R.map(为本机Array.map()
)returns一个数组。您可以生成一个对象数组,使用原始值作为键,然后将它们合并为一个对象:
const { pipe, map, mergeAll } = R
const fn = pipe(
map(k => ({ [k]: { selected: true, selectedColor: "#ff5555"} })),
mergeAll
)
const array = ['2020-06-03', '2020-06-05', '2020-06-06']
const result = fn(array)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
另一种选择是创建一个 [key, value] 数组,然后使用 R.fromPairs:
转换为一个对象
const { pipe, map, fromPairs } = R
const fn = pipe(
map(k => [k, { selected: true, selectedColor: "#ff5555"}]),
fromPairs
)
const array = ['2020-06-03', '2020-06-05', '2020-06-06']
const result = fn(array)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
对于 vanilla JS,您可以使用 Array.map()
和 Object.fromEntries()
:
const fn = arr => Object.fromEntries(
arr.map(k => [k, { selected: true, selectedColor: "#ff5555"}])
)
const array = ['2020-06-03', '2020-06-05', '2020-06-06']
const result = fn(array)
console.log(result)
我有一个这样的数组:
array = ['2020-06-03', '2020-06-05', '2020-06-06']
我想把它塑造成这样:
Object {
"2020-06-03": Object {
"selected": true,
"selectedColor": "#ff5555",
},
"2020-06-04": Object {
"selected": true,
"selectedColor": "#ff5555",
},
"2020-06-05": Object {
"selected": true,
"selectedColor": "#ff5555",
},
}
我就是这样做的:
const historyDates = map(date => {
return { selected: true, selectedColor: "#ff5555"};
}, array);
它不会工作,因为钥匙在哪里?如何使用 Ramda 表达式添加它? 我还返回了一个地图,一个新数组而不是一个充满对象的对象。
如有任何帮助,我们将不胜感激。
R.map(为本机Array.map()
)returns一个数组。您可以生成一个对象数组,使用原始值作为键,然后将它们合并为一个对象:
const { pipe, map, mergeAll } = R
const fn = pipe(
map(k => ({ [k]: { selected: true, selectedColor: "#ff5555"} })),
mergeAll
)
const array = ['2020-06-03', '2020-06-05', '2020-06-06']
const result = fn(array)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
另一种选择是创建一个 [key, value] 数组,然后使用 R.fromPairs:
转换为一个对象const { pipe, map, fromPairs } = R
const fn = pipe(
map(k => [k, { selected: true, selectedColor: "#ff5555"}]),
fromPairs
)
const array = ['2020-06-03', '2020-06-05', '2020-06-06']
const result = fn(array)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
对于 vanilla JS,您可以使用 Array.map()
和 Object.fromEntries()
:
const fn = arr => Object.fromEntries(
arr.map(k => [k, { selected: true, selectedColor: "#ff5555"}])
)
const array = ['2020-06-03', '2020-06-05', '2020-06-06']
const result = fn(array)
console.log(result)