Using variables with nested Javascript object

前端 未结 5 1697
甜味超标
甜味超标 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:45

    Actually no, because js object are seen as property bags and doing a[X] is for accessing first level properties only...

    But you could wrap the logic a['A']['AA']; // 1 in a function that does the same, like this

    //WARN... no undefined check here => todo !
    function _(o, path) {
      var tmp = o
      for (var i=0 ; i < path.length ; i++) {
        tmp = tmp[path[i]]
      }
      return tmp
    }
    
    var r = _(a, ['A', 'AA'])
    

    This is pretty much the same as other answers, but the difference is when dummy boy create object property name containing dots... Like var a = {"a.a" : 3 } is valid.

    Now, such problem would occurs maybe more often now with the help of IndexedDB to store anything locally...

    0 讨论(0)
  • 2020-12-25 15:46

    Since i also encounter this problem, i wrote also a one line util for this (ES6):

    const leaf = (obj, path) => (path.split('.').reduce((value,el) => value[el], obj))
    

    Example:

    const objSample = { owner: { name: 'Neo' } };
    const pathSample = 'owner.name';
    
    leaf(objSample, pathSample) //'Neo'
    
    0 讨论(0)
  • 2020-12-25 15:49
    function Leaf(obj,path) {
      path=path.split('.');
      var res=obj;
      for (var i=0;i<path.length;i++) obj=obj[path[i]];
      return res;
    }
    

    Leaf(a,'B')=2

    Leaf(a,'A.AA')=1

    Decorate with error handling etc. according to your needs.

    0 讨论(0)
  • 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');
    
    0 讨论(0)
  • 2020-12-25 15:51

    Not directly.

    Solution 1 - use object flattening

    Flatten object, to have new object var a = { 'A.AA' : 1; B : 2 };.

    See compressing object hierarchies in JavaScript or Flattening a complex json object for mvc binding to get the javascript function for it.

    Soution 2 - write key-path accessor

    I can see it was already addressed by Eugen. Reposted code-reviewed version:

    function Leaf(obj,path) {
      path=path.split('.');
      var res=obj;
      for (var i=0;i<path.length;i++) res=res[path[i]];
      return res;
    }
    

    Solution 3 - use eval

    var x = eval("a." + myRef); // x will be 1 for myRef == "A.AA", 2 for "B"
    

    Be careful with this solution as you may introduce some security issues. It is more of the curiosity.

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