简体   繁体   English

Map 数组 object 创建新数组

[英]Map array of object to create new array

I have the following code snippet from my app component to create a dynamic form fields:我的应用程序组件中有以下代码片段来创建动态表单字段:

 const [basics, updateBasics] = useState({ controls: { name: { elementType: "input", elementConfig: { type: "text", label: "Full name", }, value: "John Doe", }, label: { elementType: "input", elementConfig: { type: "text", label: "Profession", }, value: "Programmer", }, phone: { elementType: "input", elementConfig: { type: "tel", label: "Phone", }, value: "(912) 555-4321", }, website: { elementType: "input", elementConfig: { type: "URL", label: "Website", }, value: "https://test.url", }, summary: { elementType: "textarea", elementConfig: { type: "textarea", label: "Summary", }, value: "A summary of John Doe...", }, }, }); const formElementsArray = []; for (let key in basics.control) { formElementsArray.push({ id: key, config: controls[key], }); } console.log(formElementsArray);

[ [输出1

With the above code I get the following (shown in the picture)使用上面的代码,我得到以下内容(如图所示)

How do I generate the data in the same format with the following array of object:如何使用以下 object 数组生成相同格式的数据:

 const [profiles, setProfiles] = useState({ controls: [ { network: { elementType: "input", elementConfig: { type: "text", label: "Network", }, value: "Twitter", }, }, { username: { elementType: "input", elementConfig: { type: "text", label: "Username", }, value: "@john", }, }, { url: { elementType: "input", elementConfig: { type: "url", label: "URL", }, value: "tewt.url", }, }, ], });

As this is the array of object I couldn't work around it to generate the data in required format.由于这是 object 的数组,我无法解决它以生成所需格式的数据。

There you go:你有 go:

 const [profiles, setProfiles] = useState({ controls: [ { network: { elementType: "input", elementConfig: { type: "text", label: "Network", }, value: "Twitter", }, }, { username: { elementType: "input", elementConfig: { type: "text", label: "Username", }, value: "@john", }, }, { url: { elementType: "input", elementConfig: { type: "url", label: "URL", }, value: "tewt.url", }, }, ], }); const formElementsArray = profiles.controls.map(item =>({ id: Object.keys(item)[0], config: item[Object.keys(item)[0]], })) console.log(formElementsArray);

You can try this你可以试试这个

 obj = { controls: { name: { elementType: "input", elementConfig: { type: "text", label: "Full name", }, value: "John Doe", }, label: { elementType: "input", elementConfig: { type: "text", label: "Profession", }, value: "Programmer", }, phone: { elementType: "input", elementConfig: { type: "tel", label: "Phone", }, value: "(912) 555-4321", }, website: { elementType: "input", elementConfig: { type: "URL", label: "Website", }, value: "https://test.url", }, summary: { elementType: "textarea", elementConfig: { type: "textarea", label: "Summary", }, value: "A summary of John Doe...", }, }, } re = Object.entries(obj.controls).reduce((acc, curr) => [...acc, ...curr]) console.log({controls: re})

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

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