Using variables with nested Javascript object

前端 未结 5 1696
甜味超标
甜味超标 2020-12-25 15:08

Suppose I have this:

var a = { A : { AA : 1 }, B : 2 };

Is there a way for me to create a variable that could allow me to reference either

5条回答
  •  悲&欢浪女
    2020-12-25 15:50

    With lodash _.get function, you can access nested properties with dot syntax.

    Node server-side example:

    const _ = require('lodash'); 
    
    let item = { a: {b:'AA'}};
    
     _.get(item, 'a.b');
    

提交回复
热议问题