Getting sibling value of key in a JavaScript object literal

廉价感情. 提交于 2019-12-23 16:40:19

问题


Does anybody know of there's a way to reference the value of a sibling key in a JavaScript object literal?

so use the value of target in the beforeNext() function here:

obj: {
        target: 'li.player a.icon-tag',
        parent: 'ul#drop_list',
        beforeNext: function(){
          target.addClass('bind active');
        }
      }

回答1:


This is not a "JSON" object, but a JavaScript Object (or just "Object"). I assume that this is also contained in an Object literal as obj: { by itself is invalid syntax.

Anyway, yes you can reference properties of an object in methods with this.

beforeNext: function () {
    this.target;
}

http://jsfiddle.net/ExplosionPIlls/Q9v8r/




回答2:


In case you're dealing with plain JavaScript:

var cartoon = {"george jetson":{"son":"elroy","daughter":"judy"} }

Use the Object constructor to convert a string to an object:

cartoon["george jetson"].son = Object(cartoon["george jetson"].son)

Then bind the sibling value:

cartoon["george jetson"].son.sister = cartoon["george jetson"].daughter

And use toString to get the original value:

cartoon["george jetson"].son.toString()

references

  • Rosetta Code: Tree Traversal
  • Finding list items in XHTML
  • Recursive tree traversal in level order
  • Big O of pre order traversal, in order traversal ,post order, level order for tree


来源:https://stackoverflow.com/questions/17216496/getting-sibling-value-of-key-in-a-javascript-object-literal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!