简体   繁体   English

计算typeof值为'object'的属性在另一个对象中的数量

[英]Counting how many properties with a typeof value of 'object' are inside another object

I've been doing some iterations to obtain a certain amount of values. 我一直在进行一些迭代以获得一定数量的值。

            $.each(Obj, function(k,v){
                $.each(v, function(j,l){
                    $.each(l, function(r,s){
                        if(l.hasOwnProperty(r) && typeof s === 'object'){

                        }
                    })
                })
            }) 

In this code, you'll see that I'm iterating over an object named "Obj". 在这段代码中,您将看到我正在遍历一个名为“ Obj”的对象。 Inside of it we'll have a few amount of arrays and objects, because is a very complex structure. 在它里面,我们将有一些数组和对象,因为它是一个非常复杂的结构。 But let's cut the chatter. 但是,让我们削减the不休。 Once we arrive to this part... 一旦我们到达这一部分...

           $.each(l, function(r,s){
                if(l.hasOwnProperty(r) && typeof s === 'object'){

                }
            })

You'll see that we have a conditional. 您会看到我们有条件的。 In that conditional I'm checking for those properties "r" that have a typeof value of 'object'. 在这种情况下,我要检查那些类型为'object'的属性“ r”。 So, if I do a console check of "r" inside my conditional, I will actually see those specific properties. 因此,如果我在条件中执行控制台检查“ r”,则实际上将看到这些特定属性。

So, here's the thing. 所以,这是东西。 Why am I doing this? 我为什么要这样做? Well I'm building a variable that will ONLY store those "l" elements that have a child property with 'object' as a typeof value. 好吧,我正在构建一个变量,该变量将仅存储那些具有子属性(其对象类型为typeof值)的“ l”元素。 That's actually working, but the problem is that, for my purposes, I need this variable not only to work as intended, but also to store only the elements that have MORE THAN ONE property with 'object' as a typeof value. 那实际上是可行的,但问题是,就我的目的而言,我不仅需要此变量按预期工作,而且还需要仅存储具有多个属性且元素类型为typeof的元素。

So, let's consider this case: 因此,让我们考虑这种情况:

If I have two objects like the following... 如果我有两个如下所示的对象...

Obj1: [{"key":"value", "key":"value", "key":"[{"key":"value", "key":"value"}, { "key":"value", "key":"value"}]"}]

Obj1: [{"key":"value", "key":"value", "key":"[{"key":"value", "key":"value"}, { "key":"value", "key":"value"}]", "key":"[{"key":"value", "key":"value"}, { "key":"value", "key":"value"}]"}]

I want my variable to store ONLY the second one, because it has 2 values with objects inside of it. 我希望我的变量仅存储第二个变量,因为它具有2个值,其中包含对象。

So, In my conditional I would like to use some kind of logic that let me know not only if my "l" element has an object inside of it, but also, how many of them do I have, and if they're lesser than one, then my console log shouldn't bring them up. 因此,在我的条件下,我想使用某种逻辑,不仅让我知道“ l”元素中是否包含对象,还让我知道其中有多少个对象,以及它们是否较小而不是一个,那么我的控制台日志不应该显示它们。

Hope it's clear enough. 希望它足够清楚。

My idea is to design something that would be recursive and would not blow the stack: 我的想法是设计一些可以递归且不会破坏堆栈的东西:

    function inspect (obj,f,e,ledger) {
/*obj is the object to inspect,
f is the call back,
e is an internal reference to setTimeout,
ledger is the array that will be populated*/
                var ledger = ledger || [],
                    probObjCount = 0;
                f = f || false,
                e && e.value ? clearTimeout(e.value) : void(0);
                e = e || {};
                e.value = setTimeout(function(){f && f.call(ledger,ledger);},50);
                if(obj instanceof Array){
                    obj.forEach(function(d,i){
                        (typeof d === "object") && window.requestAnimationFrame(function(){
                            inspect(d,f,e,ledger);
                        })
                    })
                    return;
                }
                for (var i in obj) {
                    if(!obj.hasOwnProperty(i)){
                        continue;
                    }
                    if(typeof obj[i] === "object") {
                        ++probObjCount;
                        !function(obj){
                            window.requestAnimationFrame(function(){
                                inspect(obj,f,e,ledger);
                            })
                        }(obj[i]);
                    }
                }
                if(probObjCount >= 2){
                    ledger.push(obj);
                }
            }

You provide this function 2 things, an object to inspect and a callback that will be provided with a ledger (an array) that will be the list of objects that fits your criteria: 您提供此功能2件东西,一个要检查的对象和一个将与分类帐(数组)一起提供的回调,该分类帐将是符合您条件的对象的列表:

var x = [{"key1":"value", "key2":"value", "key3":[{"key4":"value", "key5":"value"}, { "key6":"value", "key7":"value"}]}];
inspect(x,function(l){console.log(l)});
//after a few seconds:
//[]

var x = [{"key1":"value", "key2":"value", "key3":[{"key4":"value", "key5":"value"}, { "key6":"value", "key7":"value"}], "key8":[{"key9":"value", "key10":"value"}, { "key11":"value", "key12":"value"}]}]
inspect(x,function(l){console.log(l)});
//[{…}]

Above 2 are your examples, now I will turn your 1st example into an example that would be accepted: 上面的2是您的示例,现在我将把您的第1个示例变成可以接受的示例:

var x = [{"key1":"value", "key2":"value", "key3":[{"key4":["value"], "key5":["value"]}, { "key6":"value", "key7":"value"}]}]
inspect(x,function(l){console.log(l)});
//[{…}] you will get {"key4":["value"], "key5":["value"]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM