首页 > 解决方案 > 根据界面格式化上层密钥的最佳方法

问题描述

实现以下目标的最佳方法是什么:

export interface Iabc {
   startDate: Date | string | null;
   endDate: Date | string | null;
   nestedSection: {
      a: string | null;
      b: boolean | null;
      c: boolean | null;
      d: Date | string | null;
   };
}

目前我正在转换嵌套部分中包含的日期,如下所示

 nestedSection: Object.entries(values.nestedSection).reduce((prev, [k, v]) => {
                  if (v instanceof Date) {
                     return {
                        ...prev,
                        [k]: moment(v).format('YYYY-MM-DD'),
                     };
                  }
                  return prev;
               }, values.nestedSection),

但是,我需要为嵌套部分的 startDate 和 endDate 执行此操作。TIA

标签: javascripttypescriptobjectinterfacereduce

解决方案


推荐阅读