gpt4 book ai didi

javascript - if else 三种情况的简写代码

转载 作者:行者123 更新时间:2023-12-02 14:37:38 26 4
gpt4 key购买 nike

我将如何继续缩短以下代码段?

if (typeof dIndex === 'undefined') {
dPolylines = [];
} else {
if (typeof rIndex === 'undefined') {
dPolylines[dIndex] = [];
} else {
dPolylines[dIndex][rIndex] = [];
}
}

我觉得这 8 行代码可以很容易地转换为最多 3 行,但我不知道如何处理这个问题。

我将在我的代码中多次需要这段代码。

最佳答案

Archer's comment应用于您的代码:

if (typeof dIndex === 'undefined') {
dPolylines = [];
} else if (typeof rIndex === 'undefined') {
dPolylines[dIndex] = [];
} else {
dPolylines[dIndex][rIndex] = [];
}

没短多少,但仍然易于理解(甚至可能比原始代码更容易理解)。

关于javascript - if else 三种情况的简写代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37296347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com