【问题标题】:Javascript: How to Iterate Object [Key: value] but skip some keys ([fastest way possible])Javascript:如何迭代对象 [键:值] 但跳过一些键([最快的方式])
【发布时间】:2016-04-14 11:04:32
【问题描述】:

如果给出如下脚本:

 <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>

我以前有时会这样做,但不知何故,我的大脑现在只是一片空白......任何建议都非常感谢。 (请使用您的jQuery 运行)

【问题讨论】:

  • 跳过一些keys的逻辑是什么?哪些键应该跳过,哪些不应该?
  • 您可以通过返回 true 来跳过 $.each 中的迭代
  • 如果您运行代码,请注意控制台中的"Undefined"
  • @DanielHigueras... jQuery 文档 (api.jquery.com/jquery.each) 说:jQuery.each( array, callback ) 你把true 放在哪里...有什么例子吗??
  • @ErickBest "返回非 false 与 for 循环中的 continue 语句相同;它将立即跳到下一次迭代。"取自您的链接

标签: javascript object key iteration


【解决方案1】:

作为JQuery docs 状态,

返回非 false 与 for 循环中的 continue 语句相同; 它将立即跳到下一次迭代。

因此,按照您的示例,您可以使用以下命令跳过具有您不需要的属性的对象:

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

【讨论】:

    【解决方案2】:

    (不是 jquery - 香草) 迭代属性并跳过特定键:(没有测试语法,但应该没问题)

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

    说明:迭代属性,如果属性确实包含在对象中但不包含在skiped 中,则执行任何操作

    【讨论】:

    • JQuery 只是 JS 之上的一个库(通常称为 Vanilla),因此您可以在任何拥有对象的地方使用。 - 我会改一些名字让你更清楚
    • 谢谢....虽然我真的很想使用$.each...更改为vanila 意味着在不同@ 中挖掘超过180 个不同的scripts 987654326@ 将 $.each 更改为 vanila 方法......这将是另一项艰巨的任务......不过谢谢!
    • Np 是有道理的,尽管您可以在每个下使用我的代码:p
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2014-12-02
    • 2018-05-25
    • 2011-09-10
    相关资源
    最近更新 更多