How to correctly organize array of objects form?
See original GitHub issueBug, Feature, or Question?
Question
At the moment there is only one example of FieldArray which handles array of strings. https://github.com/jaredpalmer/formik#fieldarray
Is it possible to implement an array of objects? In case each of friends
has firstName
, lastName
and age
for example.
What is a proper way to do it?
Issue Analytics
- State:
- Created 6 years ago
- Comments:14 (6 by maintainers)
Top Results From Across the Web
Quick Tip: How to Sort an Array of Objects in JavaScript
To sort an array of objects, use the sort() method with a compare function. A compareFunction applies rules to sort arrays by defined...
Read more >Sort an Array of Objects in JavaScript
To sort an array of objects, you use the sort() method and provide a comparison function that determines the order of objects.
Read more >Sort array of objects by string property value - Stack Overflow
The sort method can be modified to sort anything like an array of numbers, strings and even objects using a compare function. A...
Read more >Array.prototype.sort() - JavaScript - MDN Web Docs
The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted. The default...
Read more >JavaScript Problem: Sorting an Array of Objects - YouTube
The sort method of JavaScript arrays allows the use of a callback function. For many sort features, this is necessary.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Here is what I used in my app:
And here is an example validationSchema:
@latviancoder @jaredpalmer Is it possible to combine the creation of a new element with an existing
<FieldArray />
?In the same example @latviancoder posted, the button to add a new element into the array has this handler
onClick={() => arrayHelpers.push({ name: '', age: '' })}
from below:In this example this would just push
{ name: '', age: '' }
element into the array offriends
.However, what if my form is a bit more complex, in that I want to render fields for the new element that I want to push into the array of
this.props.values.friends
.The code that I imagine/hope for would be something like this (which I realize doesn’t make much sense):
Currently what I have to do is create a separate set of fields for the new element…
Which means I have to have separate validation for those new fields
new_friend_name
andnew_friend_age
, and add a special handler that will add the new element into the existingthis.props.values.friends
usingthis.props.setValues
upon successful validation.Is there a more elegant way to accomplish this, where I can put the creation of a new element under the umbrella of the
<FieldArray />
?Hopefully I’m illustrating the problem clearly enough. Chose not to create another issue as it seems closely tied to this, but certainly don’t mind creating another one if you guys think so.