Automatically create object if undefined

后端 未结 12 1045
Happy的楠姐
Happy的楠姐 2020-12-07 13:07

Is there an easy way to automatically add properties to objects if they don\'t allready exist?

Consider the following example:

var test = {}
test.hel         


        
12条回答
  •  不思量自难忘°
    2020-12-07 13:35

    let test = {};
    test = {...test, hello: {...test.hello, world: 'Hello does exist!'}};
    console.log(test);

    When using the spread operator, the value can be undefined, it'll automatically create an object.

提交回复
热议问题