Javascript 如果为空,则初始化解构道具
如果变量为null或不存在,我想将其初始化为[] 这是我的代码:Javascript 如果为空,则初始化解构道具,javascript,reactjs,destructuring,Javascript,Reactjs,Destructuring,如果变量为null或不存在,我想将其初始化为[] 这是我的代码: const { columnDates = [], rowTitles = [], last3MonthComparisonDirections = [], last3MonthComparisonMeanings = [], last3MonthComparisonValues = [], last3MonthValues = [], last12MonthValues = [], lastMonthComparisonDi
const {
columnDates = [],
rowTitles = [],
last3MonthComparisonDirections = [],
last3MonthComparisonMeanings = [],
last3MonthComparisonValues = [],
last3MonthValues = [],
last12MonthValues = [],
lastMonthComparisonDirections = [],
lastMonthComparisonMeanings = [],
lastMonthComparisonValues = [],
lastMonthValues = [],
yearToDateValues = [],
emphasisRow = [],
} = table;
现在的问题是,如果表中的columnDates
为null,则我的变量columnDates为null。我希望它在[]处初始化它,而不是在null处初始化它(如果它为null)
有什么建议吗?
谢谢大家! 仅当被解构的值是未定义的
时才会使用默认值。改变这一点是你无法做到的。您有几个选择:
不要使用解构,而是使用较新的(??
):
const columnDates=table.columnDates??[]; // 如果columnDates为'null'或'undefined',则为默认值`
... 继续为其他人。。。
如果只有columnDates
存在此问题,请对columnDates
执行上述操作,然后对其他属性使用解构。否则,如果所有值都可以为null
,则对每个属性执行上述操作
将null
值映射到undefined
,以便为null
值取默认值:
const{
columnDates=[],
行标题=[],
等
}=Object.fromEntries(Object.entries(table.map(
([key,val])=>[key,val==null?未定义:val]
));
以上映射到整个表
对象的值,并使用以前的null
值为未定义
为您构建一个新对象
您可以创建要提取的字符串属性数组,并从对象映射这些属性,而不是映射整个对象:
const key=[“columnDates”,“rowTitles”,“…];
const desiredKeys=Object.fromEntries(keys.map(
key=>[key,table[key]==null?[]:table[key]]
));
上面使用=
检查表[key]
是否等于null
或未定义
,如果是,则默认值为空的[]
数组。如果您只希望null
具有此行为,请改用严格相等==
。仅当要解构的值为未定义时才会使用默认值。改变这一点是你无法做到的。您有几个选择:
不要使用解构,而是使用较新的(??
):
const columnDates=table.columnDates??[]; // 如果columnDates为'null'或'undefined',则为默认值`
... 继续为其他人。。。
如果只有columnDates
存在此问题,请对columnDates
执行上述操作,然后对其他属性使用解构。否则,如果所有值都可以为null
,则对每个属性执行上述操作
将null
值映射到undefined
,以便为null
值取默认值:
const{
columnDates=[],
行标题=[],
等
}=Object.fromEntries(Object.entries(table.map(
([key,val])=>[key,val==null?未定义:val]
));
以上映射到整个表
对象的值,并使用以前的null
值为未定义
为您构建一个新对象
您可以创建要提取的字符串属性数组,并从对象映射这些属性,而不是映射整个对象:
const key=[“columnDates”,“rowTitles”,“…];
const desiredKeys=Object.fromEntries(keys.map(
key=>[key,table[key]==null?[]:table[key]]
));
上面使用=
检查表[key]
是否等于null
或未定义
,如果是,则默认值为空的[]
数组。如果您只希望null
具有此行为,请改用严格相等的==
。如前一个答案中所述,默认值适用于未定义的道具,但null是已定义道具的值,因此您需要手动执行这些检查
除了建议的方法之外,我还建议另外一种方法,使用代理
const表={
columnDates:null,
行标题:[1,2,3],
最近3个月比较条件:[],
Last3个月比较含义:null,
YearToDate值:[a',b',c'],
emphasisRow:空
};
var proxyObj=新代理(表{
获取:(目标,道具)=>target[prop]==null?未定义:目标[prop]
});
常数{
columnDates=[],
行标题=[],
最近3个月比较指标=[],
最近3个月比较意义=[],
最近3个月比较值=[],
Last3个月值=[],
Last12个月值=[],
lastMonthComparisonDirections=[],
lastMonthComparisonMeanings=[],
lastMonthComparisonValues=[],
LastMonthValue=[],
yearToDateValues=[],
emphasisRow=[],
}=proxyObj;
console.log(columnDates);//[]
console.log(行标题);//[1,2,3]
console.log(LastMonthValue);//[]
console.log(最近3个月比较指示);//[]
如前一个答案中所述,默认值适用于未定义的道具,但null是已定义道具的值,因此您需要手动执行这些检查
除了建议的方法之外,我还建议另外一种方法,使用代理
const表={
columnDates:null,
行标题:[1,2,3],
最近3个月比较条件:[],
Last3个月比较含义:null,
YearToDate值:[a',b',c'],
emphasisRow:空
};
var proxyObj=新代理(表{
获取:(目标,道具)=>target[prop]==null?未定义:目标[prop]
});
常数{
columnDates=[],
行标题=[],
最近3个月比较指标=[],
最近3个月比较意义=[],
最近3个月比较值=[],
最近3个月