简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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