jquery selectors for plain javascript objects instead of DOM elements

后端 未结 4 915
遇见更好的自我
遇见更好的自我 2021-02-06 11:05

I\'ve just started using jquery and I\'m really enjoying using selectors. It occurs to me that the idiom would be a very nice way to traverse object trees (e.g., JSON query resu

4条回答
  •  爱一瞬间的悲伤
    2021-02-06 11:47

    simplest and easiest way is to wrap the element with jquery and then loop it by each

    var obj = { 'foo': 1, 'bar': 2,
            'child': { 'baz': [3, 4, 5] }
          };
    
    $(obj).each(function(){
    console.log(this);
    if(this.hasOwnProperty('foo'))
    {
        console.log("hey i found " + this.foo ); 
    }
    });
    

提交回复
热议问题