How to save post meta as an array in Gutenberg?

The problem in your code is that in the updateMeta() function, the value is a string (which is whatever you typed or entered into the text field), so you can’t do the value.one or value.two.

So try this instead which uses a new parameter for the function, namely prop which is the current property in the metadata that’s being updated:

updateMeta( value, prop ) { // the 'prop' is the current property to be updated
    let meta = select( 'core/editor' ).getEditedPostAttribute( 'meta' )._metakey;

    // Make sure all props are defined. (and merge with current metadata values)
    meta = {
        one: '',
        two: '',
        ...meta,
    };

    // Then update the current property.
    meta[ prop ] = value;

    dispatch( 'core/editor' ).editPost({ meta: { _metakey: meta } });
}

Then in the TextControl‘s onChange attribute:

  • Use updateMeta( value, 'one' ) for the Field One field.

  • Use updateMeta( value, 'two' ) for the Field Two field.

And you don’t have to use the exact same code I gave above, but it’s a working (tested) example. Happy coding! 🙂