最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get result as Array in Antd Form - Stack Overflow

programmeradmin0浏览0评论

I want my form to submit price data as an array format, currently, my form has a map, look like

{data.Type && 
    <div>
      {data.Type.map((datamapped)=> 

        <div key={datamapped._id}>
        <p>{datamapped.TypeName}</p>

           <Form.Item>

          {getFieldDecorator(`price.${datamapped._id}.basePrice`)(
            <Input placeholder="Base Price"/>,
          )}

          {getFieldDecorator(`price.${datamapped._id}.higherPrice`)(
            <Input placeholder="Higher Price"/>,
          )}

        </div>
      )}
    </div>
  }

Here I mapping my Type here and included, basePrice and higherPrice fields

result is :

price: 
        { 
           '5dc2913cf9e2372b11db4252': { basePrice: '0', higherPrice: '0' },
           '5dc2a109f9e2372b11db4253': { basePrice: '0', higherPrice: '0' } 
        },

I want the above result as an array format how to do it?

I want my form to submit price data as an array format, currently, my form has a map, look like

{data.Type && 
    <div>
      {data.Type.map((datamapped)=> 

        <div key={datamapped._id}>
        <p>{datamapped.TypeName}</p>

           <Form.Item>

          {getFieldDecorator(`price.${datamapped._id}.basePrice`)(
            <Input placeholder="Base Price"/>,
          )}

          {getFieldDecorator(`price.${datamapped._id}.higherPrice`)(
            <Input placeholder="Higher Price"/>,
          )}

        </div>
      )}
    </div>
  }

Here I mapping my Type here and included, basePrice and higherPrice fields

result is :

price: 
        { 
           '5dc2913cf9e2372b11db4252': { basePrice: '0', higherPrice: '0' },
           '5dc2a109f9e2372b11db4253': { basePrice: '0', higherPrice: '0' } 
        },

I want the above result as an array format how to do it?

Share Improve this question edited Nov 8, 2019 at 18:24 CAustin 4,61414 silver badges27 bronze badges asked Nov 8, 2019 at 7:22 Harry EdwardHarry Edward 1512 gold badges2 silver badges15 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 9

Another way to get values as an array is to provide an array as FormItem name attribute like this:

<FormItem name={['wrapperName', index, 'propertyName']}>
    <Input/>,
</FormItem>

Try change datamapped._id to [index]

{data.Type && 
    <div>
      {data.Type.map((datamapped, index)=> 

        <div key={datamapped._id}>
        <p>{datamapped.TypeName}</p>

           <Form.Item>

          {getFieldDecorator(`price[${index}].basePrice`)(
            <Input placeholder="Base Price"/>,
          )}

          {getFieldDecorator(`price[${index}].higherPrice`)(
            <Input placeholder="Higher Price"/>,
          )}

        </div>
      )}
    </div>
  }

You can try this:

var json = {
  price: {
    "5dc2913cf9e2372b11db4252": { basePrice: "0", higherPrice: "0" },
    "5dc2a109f9e2372b11db4253": { basePrice: "0", higherPrice: "0" }
  }
};

json.price = Object.entries(json.price);
console.log(json);

发布评论

评论列表(0)

  1. 暂无评论