简体   繁体   中英

how to fix a linting no shadowed variable wanring

In this function below I am getting a no shadowed variable warning on line two at .reduce((nestedObject, key) . How can I resolve this?

function deepAccessUsingString(obj, key) {
    return key.split('.').reduce((nestedObject, key) => {
        if (nestedObject && key in nestedObject) {
            return nestedObject[key];
        }
        return undefined;
    }, obj);
}

Your problem is key being used in outer and inner function. You can simply give one of the key a different name.

function deepAccessUsingString(obj, key) {
    return key.split('.').reduce((nestedObject, key2) => {
        if (nestedObject && key2 in nestedObject) {
            return nestedObject[key2];
        }
        return undefined;
    }, obj);
}

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