简体   繁体   English

Javascript:如何迭代对象[键:值]但跳过一些键([最快的方法])

[英]Javascript: How to Iterate Object [Key: value] but skip some keys ([fastest way possible])

If given a script like: 如果给出类似以下的脚本:

 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test DOC</title>

<script type="text/javascript" src="../nt_scr/jq_1.9.js"></script>

<script type="text/javascript">

 var mainObj = { 0: {   "fruitName":"Lemon",
                        "fruitColor":"green",    
                        "fruitTaste":"bad"},

                 1:  {  "fruitName":"Mango",
                        "fruitColor":"Yellow",    
                        "fruitTaste":"Yammy"},
                 2:   {


                        "fruitName":"Banana",
                        "fruitColor":"Yellow",    
                        "fruitTaste":"Yammy"},


                "skip_these":   

                     {
                        "team1":"Liverpool",
                        "team2":"Manchester United"

                        } 

}

var collect_data = $.parseJSON(JSON.stringify(mainObj)),
    getFruitNames=[],getFruitColors=[], getFruitTastes=[];

$.each( collect_data,function(index,val){

     //console.log(val); //Un-comment this console.log to see the contents of 'val'

    //----->Here is the Challenge:,---------\\\
    if(/*How do you SKIP if val is something like */ "val.team1" || "val.team2"){

     getFruitNames.push(val.fruitName);
     getFruitColors.push(val.fruitColor);
     getFruitTastes.push(val.fruitTaste);

        //This works well so long as we have not yet reached the "skip_these":   
        //Once there, it reports an error because there is no "skip_these".fruitName or    "skip_these".fruitColor
        }

        console.log( getFruitNames[index])// Run a test out put :: NOTICE the "Undefined" in the Console. How to avoid that? 
        //To see well, Comment the previous console.log  <<the one on top>>
     })


</script>
</head>

<body>


</body>
</html>

I did this sometimes ago but somehow, my brain is just blank now.... Any suggestion is highly appreciated. 我有时以前做过,但是不知何故,我的大脑现在一片空白...。 (Please Run with your jQuery ) (请使用您的jQuery运行)

(not jquery - vanilla) Iterate properties and skip specific keys: (did not test for syntax but it should be ok) (不是jquery-香草)迭代属性并跳过特定键:(未测试语法,但应该可以)

    var skipped = ['keySkipped'];
    for (var someProperty in someObject) {
        if (someObject.hasOwnProperty(someProperty) && skipped.indexOf(someObject[someProperty] === -1)) {
            // do stuff
        }
    }

Explanation: iterate properties and if propertie indeed is contained by the object but not contained in skiped do whatever 说明:迭代属性,并且属性是否确实包含在对象中,但不包含在skiped执行任何操作

As JQuery docs state, 正如JQuery文档所述

Returning non-false is the same as a continue statement in a for loop; 返回非false与for循环中的continue语句相同; it will skip immediately to the next iteration. 它将立即跳至下一个迭代。

So, following your example, you can skip the objects with the properties you don't need by using the following: 因此,按照您的示例,可以使用以下内容跳过具有不需要的属性的对象:

JavaScript 的JavaScript

$.each( collect_data,function(index,val){

     //console.log(val); //Un-comment this console.log to see the contents of 'val'

    //----->Here is the Challenge:,---------\\\
    if(val.team1 || val.team2){
      return true;
    }

     getFruitNames.push(val.fruitName);
     getFruitColors.push(val.fruitColor);
     getFruitTastes.push(val.fruitTaste);

        //This works well so long as we have not yet reached the "skip_these":   
        //Once there, it reports an error because there is no "skip_these".fruitName or    "skip_these".fruitColor


        console.log( getFruitNames[index])//No "Undefined"
     })

https://plnkr.co/edit/9vOACIpnlWRtSWjmAm5x?p=preview https://plnkr.co/edit/9vOACIpnlWRtSWjmAm5x?p=preview

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

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