Replace a value if null or undefined in JavaScript

前端 未结 5 1387
刺人心
刺人心 2020-11-30 01:35

I have a requirement to apply the ?? C# operator to JavaScript and I don\'t know how. Consider this in C#:

int i?=null;
int j=i ?? 10;//j is now         


        
相关标签:
5条回答
  • 2020-11-30 01:55

    Logical nullish assignment, 2020+ solution

    A new operator is currently being added to the browsers, ??=. This is equivalent to value = value ?? defaultValue.

    ||= and &&= are also coming, links below.

    This checks if left side is undefined or null, short-circuiting if already defined. If not, the left side is assigned the right-side value.

    Basic Examples

    let a          // undefined
    let b = null
    let c = false
    
    a ??= true  // true
    b ??= true  // true
    c ??= true  // false
    
    // Equivalent to
    a = a ?? true
    

    Object/Array Examples

    let x = ["foo"]
    let y = { foo: "fizz" }
    
    x[0] ??= "bar"  // "foo"
    x[1] ??= "bar"  // "bar"
    
    y.foo ??= "buzz"  // "fizz"
    y.bar ??= "buzz"  // "buzz"
    
    x  // Array [ "foo", "bar" ]
    y  // Object { foo: "fizz", bar: "buzz" }
    

    Functional Example

    function config(options) {
        options.duration ??= 100
        options.speed ??= 25
        return options
    }
    
    config({ duration: 555 })   // { duration: 555, speed: 25 }
    config({})                  // { duration: 100, speed: 25 }
    config({ duration: null })  // { duration: 100, speed: 25 }
    

    ??= Browser Support Nov 2020 - 77%

    ??= Mozilla Documentation

    ||= Mozilla Documentation

    &&= Mozilla Documentation

    0 讨论(0)
  • 2020-11-30 02:05

    I spotted half of the problem: I can't use the 'indexer' notation to objects (my_object[0]). Is there a way to bypass it?

    No; an object literal, as the name implies, is an object, and not an array, so you cannot simply retrieve a property based on an index, since there is no specific order of their properties. The only way to retrieve their values is by using the specific name:

    var someVar = options.filters.firstName; //Returns 'abc'
    

    Or by iterating over them using the for ... in loop:

    for(var p in options.filters) {
        var someVar = options.filters[p]; //Returns the property being iterated
    }
    
    0 讨论(0)
  • 2020-11-30 02:10

    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.

    0 讨论(0)
  • 2020-11-30 02:10

    ES2020 Answer

    The new Nullish Coalescing Operator, is finally available on JavaScript, though browser support is limited. According to the data from caniuse, only 48.34% of browsers are supported (as of April 2020).

    According to the documentation,

    The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.

    const options={
      filters:{
        firstName:'abc'
      } 
    };
    const filter = options.filters[0] ?? '';
    const filter2 = options.filters[1] ?? '';
    

    This will ensure that both of your variables will have a fallback value of '' if filters[0] or filters[1] are null, or undefined.

    Do take note that the nullish coalescing operator does not return the default value for other types of falsy value such as 0 and ''. If you wish to account for all falsy values, you should be using the OR operator ||.

    0 讨论(0)
  • 2020-11-30 02:14

    Destructuring solution

    Question content may have changed, so I'll try to answer thoroughly.

    Destructuring allows you to pull values out of anything with properties. You can also define default values when null/undefined and name aliases.

    const options = {
        filters : {
            firstName : "abc"
        } 
    }
    
    const {filters: {firstName = "John", lastName = "Smith"}} = options
    
    // firstName = "abc"
    // lastName = "Smith"
    

    NOTE: Capitalization matters

    If working with an array, here is how you do it.

    In this case, name is extracted from each object in the array, and given its own alias. Since the object might not exist = {} was also added.

    const options = {
        filters: [{
            name: "abc",
            value: "lots"
        }]
    }
    
    const {filters:[{name : filter1 = "John"} = {}, {name : filter2 = "Smith"} = {}]} = options
    
    // filter1 = "abc"
    // filter2 = "Smith"
    

    More Detailed Tutorial

    Browser Support 92% July 2020

    0 讨论(0)
提交回复
热议问题