DarkAjaxValentin提出了一个问题:Replace a value if null or undefined in JavaScript,或许与您遇到的问题类似。
回答者MojoFilterGumbo给出了该问题的处理方式:
Here’s the JavaScript equivalent:
var i = null;
var j = i || 10; //j is now 10
Note that the logical operator || does not return a boolean value but the first value that can be converted to true.
Additionally use an array of objects instead of one single object:
var options = {
filters: [
{
name: 'firstName',
value: 'abc'
}
]
};
var filter = options.filters[0] || ''; // is {name:'firstName', value:'abc'}
var filter2 = options.filters[1] || ''; // is ''
That can be accessed by index.
希望本文对你有帮助,欢迎支持JavaScript中文网