[英]I have an array that I want to grab some values from and add them as a object to a inititalValues in a Ant Design Form Componenet
I have a Ant Design Form Component that takes some initital vales.我有一个 Ant 设计表单组件,它需要一些初始值。 I need to populate them from an array, but I it needs to be in this format.
我需要从数组中填充它们,但我需要采用这种格式。
<Form
{...layout}
name="basic"
initialValues={{ "name-0": "fdsanfdsk", "name-1": "fdsafjasf9" }} // I need the name values
// from the array here
onFinish={onFinish}
onFinishFailed={onFinishFailed}
>
My array looks like so我的数组看起来像这样
[{
"__typename": "FormInputVal",
"name": "name",
"_id": "291541872966369805",
"type": "text"
}, {
"__typename": "FormInputVal",
"name": "name",
"_id": "291541888089981453",
"type": "text"
}, {
"__typename": "FormInputVal",
"name": "test",
"_id": "291644307943719437",
"type": "text"
}, {
"__typename": "FormInputVal",
"name": "test",
"_id": "291649317517656585",
"type": "text"
}, {
"__typename": "FormInputVal",
"name": "test",
"_id": "291649666387280392",
"type": "text"
}, {
"__typename": "FormInputVal",
"name": "test",
"_id": "291651264892109325",
"type": "text"
}, {
"__typename": "FormInputVal",
"name": "test",
"_id": "291651422325309961",
"type": "text"
}, {
"__typename": "FormInputVal",
"name": "test",
"_id": "291651568083665417",
"type": "text"
}, {
"__typename": "FormInputVal",
"name": "test",
"_id": "291653619769410057",
"type": "text"
}]
I just need the names from the array, and inserted into the initalValues object in the Form Component, with a -0, -1, -2 tacked on.我只需要数组中的名称,并将其插入到表单组件中的 initalValues object 中,并添加 -0、-1、-2。
I have tried the following我试过以下
const test = formState?.map((item, idx) => {
return `${item.name}-${idx}:"fdsjfs"`;
});
and then I end up with another array, and I have tried turning that array test into a Object with Object.assign({},test)
but then it has keys on it.然后我最终得到另一个数组,我尝试将该数组测试转换为
Object.assign({},test)
的 Object 但它上面有键。
Any help would be greatley appreciated任何帮助将不胜感激
You might consider using the reduce
method.您可以考虑使用
reduce
方法。
const test = formState.reduce((obj, item, idx) => {
return { ...obj, [`${item.name}-${idx}`]: 'fdsjfs' };
}, {});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.