简体   繁体   English

反应最终表单拆分字段值

[英]React final form split field values

I have a react and redux application with react final form where I have a field named 'cars' which is a select and on spy fire a submit event if data is dirty.我有一个 react 和 redux 应用程序,它带有 react final 形式,其中我有一个名为“cars”的字段,它是一个选择,如果数据脏,则在 spy 上触发提交事件。 All good with the values and stuff but I need to split that into the types so the form returns something like coupe: ['BMW'], sedan: ['Audi'] not just the field name with values such as cars: ['BMW', 'Audi']值和东西都很好,但我需要将其拆分为类型,以便表单返回类似coupe: ['BMW'], sedan: ['Audi']的字段名称,而不仅仅是带有cars: ['BMW', 'Audi']等值的字段名称cars: ['BMW', 'Audi']

{
  type: "Coupe"
  value: "BMW"   
},
{
  type: "Sedan"
  value: "Audi"   
}

Maybe anyone has a suggestion how to mutate that and when inside react final forms?也许有人建议如何改变它以及何时在内部反应最终形式? or should it be done inside a reducer or saga?还是应该在减速器或传奇中完成?

Assuming you get your form data as假设您将表单数据设为

[
    {
        type: "Coupe",
        value: "BMW"
    }, {
        type: "Sedan",
        value: "Audi"
    }
]

and you need to turn type value into the key name, so you get你需要把type值变成键名,所以你得到

[{ coupe: 'BMW'}, {sedan: 'Audi'}]

You may use Array.prototype.map() (inside your submit handler, or reducer, or wherever it works better for you):您可以使用Array.prototype.map() (在您的提交处理程序、reducer 或任何更适合您的地方):

 const formData = [{type:"Coupe",value:"BMW"},{type:"Sedan",value:"Audi"}], remappedData = formData.map(({type,value}) => ({[type.toLowerCase()]: value})) console.log(remappedData)
 .as-console-wrapper{min-height:100%;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM