简体   繁体   English

数组减少逗号运算符无序列的意外使用

[英]Array reduce Unexpected use of comma operator no-sequences

I am getting an "Unexpected use of comma operator no-sequences" warning -- on the.reduce - but I am not sure how to resolve this.我在 the.reduce 上收到“意外使用逗号运算符无序列”警告 - 但我不确定如何解决此问题。

const getQueryParams = () => 
  this.props.location.search
    .replace('?', '')
    .split('&')
    .reduce((r,e) => (r[e.split('=')[0]] = decodeURIComponent(e.split('=')[1]), r), {});

The code quoted uses (some would say ab uses) the comma operator in order to avoid using the function body form of an arrow function. The minimal change to remove the comma operator is to put {} around the function body and do an explicit return :引用的代码使用(有些人会说ab使用)逗号运算符以避免使用箭头 function 的 function 主体形式。删除逗号运算符的最小更改是将{}放在 function 主体周围并进行显式return :

const getQueryParams = () => 
    this.props.location.search
        .replace('?', '')
        .split('&')
        .reduce((r,e) => {
            r[e.split('=')[0]] = decodeURIComponent(e.split('=')[1]);
            return r;
        }, {});

As a matter of style, though, I'd suggest not using reduce there at all.不过,就风格而言,我建议根本不要在那里使用reduce (I have a fair bit of company disliking reduce outside of Functional Programming with predefined, reusable reducers.) (我有相当多的公司不喜欢使用预定义的、可重用的 reducer 在函数式编程之外进行reduce 。)

In that code, the reduce is just a loop;在该代码中, reduce只是一个循环; the accumulator never changes, it's always the same object. So I'd just use a loop:累加器永远不会改变,它始终是相同的 object。所以我只使用一个循环:

const getQueryParams = () => {
    const result = {};
    for (const e of this.props.location.search.replace("?", "").split("&")) {
        result[e.split("=")[0]] = decodeURIComponent(e.split("=")[1]);
    }
    return result;
};

I'd probably also remove the redundant call to split :我可能还会删除对split的冗余调用:

const getQueryParams = () => {
    const result = {};
    for (const e of this.props.location.search.replace("?", "").split("&")) {
        const [key, value] = e.split("=");
        result[key] = decodeURIComponent(value);
    }
    return result;
};

Finally, both keys and values in query strings are URI-encoded, so decodeURIComponent should be used on both:最后,查询字符串中的键值都是 URI 编码的,因此应在两者上使用decodeURIComponent

const getQueryParams = () => {
    const result = {};
    for (const e of this.props.location.search.replace("?", "").split("&")) {
        const [key, value] = e.split("=");
        result[decodeURIComponent(key)] = decodeURIComponent(value);
    }
    return result;
};

It'll work without if the keys are just alphanumerics and such, but it's not correct .如果键只是字母数字等,它会在没有键的情况下工作,但它是不正确的。


Stepping back from the syntax, though, you don't need to invent your own function for parsing query string parameters.但是,从语法上退一步,您不需要发明自己的 function 来解析查询字符串参数。 Browsers already have one :浏览器已经有一个

const getQueryParams = () => Object.fromEntries(
    new URLSearchParams(this.props.location.search)
    .entries()
);

Live Example:现场示例:

 const search = "?bar=Testing%201%202%203&baz=2"; console.log( Object.fromEntries( new URLSearchParams(search).entries() ) );

You can rewrite the reduce call, so to avoid an assignment expression (and comma operator), turning the arrow function expression syntax into block syntax (see arrow function expression ):您可以重写reduce调用,以避免赋值表达式(和逗号运算符),将箭头 function 表达式语法转换为块语法(参见箭头 function 表达式):

.reduce((r,e) => {
     r[e.split('=')[0]] = decodeURIComponent(e.split('=')[1]);
     return r;
}, {});

Another approach would be to use Object.assign :另一种方法是使用Object.assign

 let search = ["item=test","name=code%28","foo=%20bar"] let result = search.reduce((r,e) => Object.assign(r,{[e.split('=')[0]]: decodeURIComponent(e.split('=')[1])}), {}); console.log(result)

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

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