小编Mil*_*los的帖子

如何重命名数组内的对象键

我有一个像这样的对象数组:

const customers = [
  {
    customer_name: 'Negan', 
    customer_age: 45, 
    customer_weapon: 'Bat',
    customer_email: 'negan@sanctuary.com',
    customer_city: 'Washington' 
  },
  {
    customer_name: 'Daryl', 
    customer_age: 41, 
    customer_weapon: 'Crossbow',
    customer_email: 'daryl.dixon@kickass.com',
    customer_city: 'Atlanta' 
  },
  {
    customer_name: 'Rick', 
    customer_age: 45, 
    customer_weapon: 'Magnum 357',
    customer_email: 'rick@alexandria.com',
    customer_city: 'King County' 
  },
]
Run Code Online (Sandbox Code Playgroud)

我想用这些键替换对象内的所有键:

const newKeys = [
   'firstname',
   'age',
   'weapon',
   'email',
   'city'
]
Run Code Online (Sandbox Code Playgroud)

做这个的最好方式是什么?一个例子将不胜感激!

javascript arrays object ecmascript-6

19
推荐指数
1
解决办法
362
查看次数

“Date[]”类型的参数不可分配给“number[]”类型的参数

我有一个带有这样日期的对象

const dateObject = {
     "BaseTransactions.maxOccurredAt": "2021-01-19T20:30:45.000",
     "BaseTransactions.minOccurredAt": "2016-12-28T12:55:37.000",
     "EmailCampaignEvents.maxOccurredAt": "2021-04-13T11:32:50.000",
     "EmailCampaignEvents.minOccurredAt": "2021-04-09T12:15:26.000",
};
Run Code Online (Sandbox Code Playgroud)

我用它创建了一个数组,其中的日期如下

const arrayOfDates: Date[] = Object.values(dateObject).map(
    (date: string | unknown) => new Date(date as string)
  );
Run Code Online (Sandbox Code Playgroud)

我正在尝试从该数组中获取最小和最大日期,如下所示

const minDate = new Date(Math.min.apply(null, arrayOfDates));
const maxDate = new Date(Math.max.apply(null, arrayOfDates));
Run Code Online (Sandbox Code Playgroud)

arrayOfDates但我在Math.min 和 Math.max 内部收到打字稿错误。错误是这样说的Argument of type 'Date[]' is not assignable to parameter of type 'number[]'. Type 'Date' is not assignable to type 'number'.ts(2345)。我究竟做错了什么?

arrays date typescript react-typescript

3
推荐指数
1
解决办法
1289
查看次数

控制 Material UI 中的多个复选框

我有一个带有几个复选框的下拉菜单,我试图在每个复选框上添加 onClick 事件。但是当我选中其中一个复选框时,所有复选框都会被选中。这就是我尝试做的:

const [isChecked, setIsChecked] = React.useState(false);

const isCheckboxChecked = () => {
    setIsChecked(isChecked ? false : true)
}

return menuItems.map((item, index) => (
        <React.Fragment>
            <MenuItem
                value={item.value}
                selected={item.value === value}
                key={index}
            >
                <Checkbox
                    key={index}
                    checked={isChecked}
                    onClick={() => isCheckboxChecked()}
                >
                    <Label>{item.label}</Label>
                </Checkbox>
            </MenuItem>
            {index === 2 || index === 3 ? <hr /> : null}
        </React.Fragment>
    ));
Run Code Online (Sandbox Code Playgroud)

据我了解,所有复选框都使用相同的状态isChecked,我想知道如何使它们独立。

checkbox reactjs material-ui

0
推荐指数
1
解决办法
1万
查看次数