简体   繁体   English

我有一个数组,我想从中获取一些值并将它们作为 object 添加到 Ant 设计表单组件中的初始值中

[英]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.

相关问题 如何以 JSON 格式从多个 object 中获取值并将它们存储在数组中? - How Do I grab values from multiple object in a JSON format and store them in an array? 我有多个 arrays 的单个 object 具有多个值,我想获取这些值并显示它们 - I have muliple arrays of single object with multiple values and i want to get these values and display them 我有一个对象数组和一个对象,我想遍历对象,同时将对象值与数组中的值匹配 - I have an array of objects and an object I want to loop through an object while matching object values with the values in the array 如何使用Redux从一系列复选框中获取值以与其他一些组件共享? - How can I grab values to share with some other components from an array of checkboxes using Redux? 如何从数组中删除所需的对象,然后使用.forEach将它们添加到另一个数组中? - How do I remove the objects I want from an array and add them to another array using .forEach? 如何跟踪“Ant Design-Dynamic Form Item”并将所有值存储在单个对象状态中? - how can i keep track of "Ant Design- Dynamic Form Item" and store all values in a single object state? 我有一组对象,如果其中一个键匹配(不覆盖初始属性),我想将它们组合成一个 object - I have an array of objects and want to combine them into one object IF the one of the keys match (without overwriting the initial properties) Ant 设计 4 验证来自数组的表单项 - Ant design 4 validate form items from array 如何从预定义的对象呈现 Ant Design Icon - How do I render an Ant Design Icon from a predefined Object 如何自定义 Ant 设计的表格? - How can I customised the form of Ant Design?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM