我如何将嵌套数组与 ramda 合并到一个新对象中?
how i can merge nested array with in a new object with ramda?
有一个对象保存与 UI 相关的变量。
const data = [
{ color: [{ primary: "#444", secondary: "#dede", light: "#fff" }] },
{ size: [12,14,16,18,20] },
...
];
我想将此对象设为单个数组,例如:
const UI = [
{ colorPrimary: "#444" },
{ colorSecondary: "#dede" },
{ colorLight: "#fff" },
{ size0: 12 },
{ size1: 14 },
{ size2: 16 },
{ size3: 18 },
{ size4: 20 },
...
];
我如何使用 ramda 执行此操作?
由于我的评论中表达的原因,我没有一个干净的方法来处理提供的数据:
How would you distinguish between a property like color
, where you seem to ignore the array holding the data, and one like size
, where the array indices become part of the keys?
但只要对您的数据稍作调整,就可以:
const data = [
{ color: { primary: "#444", secondary: "#dede", light: "#fff" } },
{ size: [12,14,16,18,20] },
];
相当简单:
const initialCap = str => toUpper(head(str)) + tail(str);
const collectProps = pipe(
map(map(toPairs)),
apply(merge),
mapObjIndexed((pairs, key) => map(
pair => objOf(key + initialCap(pair[0]), pair[1]), pairs
)),
values,
apply(concat)
);
collectProps(data);
您可以在 Ramda REPL.
上看到实际效果
我对你的数据来源一无所知,所以我不知道你是否可以管理这一切。但如果可以的话,这看起来应该可行。
有一个对象保存与 UI 相关的变量。
const data = [
{ color: [{ primary: "#444", secondary: "#dede", light: "#fff" }] },
{ size: [12,14,16,18,20] },
...
];
我想将此对象设为单个数组,例如:
const UI = [
{ colorPrimary: "#444" },
{ colorSecondary: "#dede" },
{ colorLight: "#fff" },
{ size0: 12 },
{ size1: 14 },
{ size2: 16 },
{ size3: 18 },
{ size4: 20 },
...
];
我如何使用 ramda 执行此操作?
由于我的评论中表达的原因,我没有一个干净的方法来处理提供的数据:
How would you distinguish between a property like
color
, where you seem to ignore the array holding the data, and one likesize
, where the array indices become part of the keys?
但只要对您的数据稍作调整,就可以:
const data = [
{ color: { primary: "#444", secondary: "#dede", light: "#fff" } },
{ size: [12,14,16,18,20] },
];
相当简单:
const initialCap = str => toUpper(head(str)) + tail(str);
const collectProps = pipe(
map(map(toPairs)),
apply(merge),
mapObjIndexed((pairs, key) => map(
pair => objOf(key + initialCap(pair[0]), pair[1]), pairs
)),
values,
apply(concat)
);
collectProps(data);
您可以在 Ramda REPL.
上看到实际效果我对你的数据来源一无所知,所以我不知道你是否可以管理这一切。但如果可以的话,这看起来应该可行。