简体   繁体   English

将多个“键值”对作为参数传递给 JS 函数

[英]Passing multiple "key-value" pairs as a parameter to a JS function

I have a date range picker, and I want to pass the two dates (startDate, endDate) to handleFilterChange, which builds an filter object to save in session storage.我有一个日期范围选择器,我想将两个日期(startDate、endDate)传递给 handleFilterChange,它会构建一个过滤器对象以保存在会话存储中。 Currently I try to call the function, giving parameters as two arrays, which obviously doesn't work.目前我尝试调用该函数,将参数作为两个数组提供,这显然不起作用。 Calling the function twice also didn't work.两次调用该函数也不起作用。

Is this thing even possible and if, then what would be a correct way to pass these two dates to this function?这件事甚至可能吗,如果,那么将这两个日期传递给这个函数的正确方法是什么?

handleFilterChange句柄过滤器更改

const handleFilterChange = (value, title) => {
    clearPreviousResults();
    const newFilters = {
        ...filters,
        [title]: value,
    };

    saveFilterValue(newFilters, FILTERS_STORAGE_KEY);
    setFilters(newFilters)

date picker日期选择器

const handleDateRangeChange = dates => {
    if (!dates || dates.length <= 1) {
        return;
    }
    handleFilterChange( ["startDate", "endDate"], [dates[0], dates[1]] );
    };

I am assuming you are trying to save an object in the local storage or session.我假设您正在尝试将对象保存在本地存储或会话中。 I would rather send the whole dates array to handleFilterChange function.我宁愿将整个日期数组发送到 handleFilterChange 函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM