Given a Formik context containing an array of objects:
{
myArrayOfObjects: {
property: string
}[]
}
I would like to get an array of the values of property. I don't, however, have any idea whether this is possible. For further reference, I initially attempted myArrayOfObjects.property and myArrayOfObjects[].property in the hopes it would magically work, but unfortunately it does not.
The end goal here is to use this in tandem with a react-select multiselect field.
Is this possible, and if not, what would be the proper way to work this out properly?
This is on formik v2.2.6
You can transform the data to get the array. There are possible better ways to do it, but this is the idea:
const getPropertyValues = (values: InnerObject[]) => {
const newArray: string[] = [];
values.map((x: InnerObject) => {
newArray.push(x.properties);
});
return newArray;
};
I did a CodeSandbox so you can try it. I use that same function in 2 places:
values of the form as an string[]SelectComponent to create the options that will be displayed on the react-select component