jQuery对象的递归迭代 [英] jQuery recursive iteration over objects

查看:100
本文介绍了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)} );

$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) {
///做东西
}

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆