typescript 如何在.map()中将对象的字符串值转换为动态值?

bjp0bcyl  于 2023-10-22  发布在  TypeScript
关注(0)|答案(1)|浏览(134)

我有一个带有defaultValue的formInputItems数组,当我Map这些数组时,我希望将defaultValue转换为动态值,因为它将其识别为字符串。所以不是返回实际的电影标题,而是返回对象中的movie.title。movie对象是作为一个 prop 传递给cardform的,我如何让它识别它?因此,当我MapformInputItems时,我希望它识别defaultValue={${movie.plot}}而不是defaultValue ='movie.plot'
我有一个对象数组作为输入:
string.js

export const formInputItems = [
            {
                label: 'title',
                defaultValue: 'movie?.title',
            },
            {
                label: 'plot',
                defaultValue: 'movie?.plot',
            },
]

Map:

const CardForm = ({ movie }: CardFormProps) => {
    const router = useRouter();
    const {
        isEditing,
        setIsEditing,
        formData,
        setFormData,
        openForm,
        setOpenForm,
    } = useGlobalContext();

    return (
        <div className='w-full h-full bg-black/80 backdrop-blur-md absolute top-0 left-0 flex items-center justify-center'>
            <form
                onSubmit={submitHandler}
                className='bg-primary p-10 max-w-[450px] w-full flex flex-col gap-4 rounded-md'>
                <TitleTwo>{isEditing ? 'Edit Movie' : 'Add Movie'}</TitleTwo>
                {formInputItems.map(({ label, defaultValue }, index) =>
                    isEditing && (
                        <EditInput
                            key={index}
                            htmlFor={label}
                            id={label}
                            defaultValue={defaultValue}
                            onChange={(e: any) => onChangeHandler(e, label)}>
                            {label}
                        </EditInput>
                    ) 
                )}
    
            </form>
        </div>
    );
};

export default CardForm;
jm81lzqq

jm81lzqq1#

使用动态变量进行控制几乎是不可能的,因为它需要首先定义。另一方面,动态控制对象的属性是可能的。

export const formInputItems = [
            {
                label: 'title',
                defaultValue: 'title',
            },
            {
                label: 'plot',
                defaultValue: 'plot',
            },
]
const CardForm = ({ movie }: CardFormProps) => {
    const router = useRouter();
    const {
        isEditing,
        setIsEditing,
        formData,
        setFormData,
        openForm,
        setOpenForm,
    } = useGlobalContext();


    return (
        <div className='w-full h-full bg-black/80 backdrop-blur-md absolute top-0 left-0 flex items-center justify-center'>
            <form
                onSubmit={submitHandler}
                className='bg-primary p-10 max-w-[450px] w-full flex flex-col gap-4 rounded-md'>
                <TitleTwo>{isEditing ? 'Edit Movie' : 'Add Movie'}</TitleTwo>
                {formInputItems.map(({ label, defaultValue }, index) =>
                    isEditing && (
                        <EditInput
                            key={index}
                            htmlFor={label}
                            id={label}
                            defaultValue={movie[defaultValue]}
                            onChange={(e: any) => onChangeHandler(e, label)}>
                            {label}
                        </EditInput>
                    ) 
                )}
    
            </form>
        </div>
    );
};

export default CardForm;

使用movie[defaultValue]时可能会收到类型错误,因此应定义或强制转换类型

相关问题