jQuery对对象的递归迭代

jQuery对对象的递归迭代

本文介绍了jQuery对对象的递归迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天我想我在 jQuery 中看到了一个对象迭代器,它有一个可以设置为递归迭代子对象的标志.我认为它是 jQuery.each() 的一部分,但现在我在文档中看不到该功能.

The other day I thought I saw an object iterator in jQuery that had a flag that could be set to recursively iterate over child objects. I thought it was part of jQuery.each(), but now I don't see that capability in the docs.

jQuery中有没有这样的迭代器可以自动递归?

Is there any such iterator in jQuery that can be automatically recursive?

(我知道如何用 javascript 做到这一点.只是想知道我是否真的看到了我以为我看到的东西.)

(I know how to do it in javascript. Just wondering if I actually saw what I thought I saw.)

非常感谢!

明确地说,我正在考虑一种实用方法,例如 jQuery.each(),它将递归地遍历 javascript 对象及其嵌套对象.

To be clear, I was thinking of a utility method like jQuery.each() that will iterate recursively over javascript objects and their nested objects.

鉴于下面的示例,each() 方法将遍历所有对象,包括 myobj.obj2.key2 中的嵌套对象.

Given the example below, the each() method would iterate over all objects, including the nested one in myobj.obj2.key2.

我可以发誓我在 jQuery 文档中看到了一些关于此的内容,但现在我找不到了.

I could have sworn that I saw something in jQuery docs about that, but now I can't find it.

谢谢.

var myobj = {
    obj1: {key1:'val1', key2:'val2'},
    obj2: {key1:'val1', key2: {nest1:'val1', nest2:'val2', nest3:'val3'}},
    obj3: {key1:'val1', key2:'val2'}
}

$jQuery.each(myobj, function(key,val) {
    // Code to run over each key/val pair
    // Does so recursively to include all nested objects
})

推荐答案

.find('selector') 方法基本上是 .children() 的递归版本,它将找到匹配选择器的任何后代对象,而不是到 .children() ,它只在第一级后代中找到对象.

The .find('selector') method is basically a recusive version of .children(), and will find any descendant object that matched the selector, as opposed to .children() which only finds objects in the first level of descendants.

第二次编辑(我第一次措辞很糟糕,代码有点混乱!):

2nd EDIT (I phrased badly the first time, and messed up the code a bit!):

好吧,我不认为这个功能作为一个标志是有意义的:你可以很高兴地永远递归通过那个对象(相信我,我打破了 firefox 这样做),所以你需要某种交互来确保你仅当子对象是有效的递归候选时才会递归.

Ok, I don't think this functionality as a flag would make sense: you can quite happily recurse through that object forever (believe me, I broke firefox doing it), so you need some sort of interaction to make sure you only recurse when the child object is a valid recursion candidate.

你需要做的只是像这样拆分函数:

What you need to do is simply split the function like so:

var myobj = {
  obj1: {
    key1: 'val1',
    key2: 'val2'
  },
  obj2: {
    key1: 'val1',
    key2: {
      nest1: 'val1',
      nest2: 'val2',
      nest3: 'val3'
    }
  },
  obj3: {
    key1: 'val1',
    key2: 'val2'
  }
}

$jQuery.each(myobj, function(key, val) {
  recursiveFunction(key, val)
});

function recursiveFunction(key, val) {
  actualFunction(key, val);
  var value = val['key2'];
  if (value instanceof Object) {
    $.each(value, function(key, val) {
      recursiveFunction(key, val)
    });
  }

}

function actualFunction(key, val) {
  /// do stuff
}

这篇关于jQuery对对象的递归迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 21:11