本文介绍了使用map()和filter()从嵌套数组映射的新数组中删除未定义的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建多个变量以供以后使用,每个变量代表一系列嵌套数组(基本上是一个用于多个项目的数据数组)中的一个特定数组.

I'm trying to build several variables to be used later, each representing a specific array from within a series of nested arrays (basically a data array being used for several projects).

当我选择要使用map()的数组时,我得到了一个新的数组,该数组在我未映射的其他每个数组中都包含undefined.这当然是有道理的,因为该数组仍具有索引的空间,那里的特定值只是未映射.不过,我在努力的地方是清理该新阵列以删除这些undefined条目.我正在尝试使用filter(),但是我对其他方法持开放态度.无论采用哪种方法,我都无法消除所有虚假的值,因为其中一些将需要包括在内.我只需要删除undefined条目.

When I get to the selection of the array I need using map(), I get the new array containing undefined in each of the other parts of the array I wasn't mapping. This makes sense of course because the array still had the space indexed, the specific values there just weren't mapped. Where I'm struggling though is cleaning up that new array to remove those undefined entries. I'm trying to use filter(), but I'm open to alternative methods. Regardless of which method, I cannot eliminate all falsy values as some of these will need to be included. I only need to remove undefined entries.

var data = {
  "cert_stat": [{
      Month: "Nov-2015",
      loOP_Percent_Cert: 87.1,
      loOP_Percent_Exp: 9.7,
      loOP_Percent_None: 3.2,
      doW_R_Percent_Cert: 62.7,
      doW_R_Percent_Exp: 20.2,
      doW_R_Percent_None: 17.3
    },
    {
      Month: "Feb-2016",
      loOP_Percent_Cert: 83.9,
      loOP_Percent_Exp: 19.9,
      loOP_Percent_None: 3.2,
      doW_R_Percent_Cert: 61.1,
      doW_R_Percent_Exp: 22.2,
      doW_R_Percent_None: 16.7
    }
  ],
  "reg_pk_xp": [{
      "Quarter": "Y1Q1",
      "Question_5": [{
          "Yes": 66
        },
        {
          "No": 7
        },
        {
          "Not Sure": 28
        },
        {
          "Total": 101
        }
      ],
      "Question_6": [{
          "Yes": 66
        },
        {
          "No": 7
        },
        {
          "Not Sure": 28
        },
        {
          "Total": 101
        }
      ]
    },
    {
      "Quarter": "Y1Q2",
      "Question_5": [{
        "Yes": 30
      }, {
        "No": 5
      }, {
        "Not Sure": 13
      }, {
        "Total": 48
      }],
      "Question_6": [{
        "Yes": 30
      }, {
        "No": 5
      }, {
        "Not Sure": 13
      }, {
        "Total": 48
      }]
    }
  ]
};

var q5_yes = data.reg_pk_xp.map(function(e) {
  return e.Question_5.map(function(e) {
    return e.Yes;
  });
});
console.log("Question_5 YES with UNDEF entries", q5_yes);


var q5_yes_filtered = q5_yes.filter(function(val) {
  return val !== undefined;
});
console.log("Question_5 YES only", q5_yes_filtered);

/*    IGNORE, PREVIOUS TEST 
document.getElementById("test").innerHTML = data.reg_pk_xp.map(function(e) {
  return e.Question_5.map(function(e) {
    return e.Yes;
  });
});
      /PREVIOUS TESTS           */


q5_yes = q5_yes.filter(function(e) {
  return e !== undefined;
});
document.getElementById("test").innerHTML = q5_yes;
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>TEST FILE</title>
</head>

<body>
  <div id="test">TEST</div>
  <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
  <script src="data.js"></script>
  <!--data.js file is normally a seperate, but local file-->
</body>

</html>

我认为filter()方法没有发挥作用(在q5_yes数组上),但是我无法弄清楚.

I'm thinking that the filter() method isn't acting where I think it is (on the q5_yes array), but I can't figure it out.

推荐答案

过滤器不起作用,因为q5_yes是二维数组,并且undefined元素位于嵌套数组中.但是,您正在过滤顶级数组.

The filter isn't working because q5_yes is a 2-dimensional array, and the undefined elements are in the nested arrays. But you're filtering the top-level array.

var data = {
  "reg_pk_xp": [{
      "Quarter": "Y1Q1",
      "Question_5": [{
          "Yes": 66
        },
        {
          "No": 7
        },
        {
          "Not Sure": 28
        },
        {
          "Total": 101
        }
      ],
      "Question_6": [{
          "Yes": 66
        },
        {
          "No": 7
        },
        {
          "Not Sure": 28
        },
        {
          "Total": 101
        }
      ]
    },
    {
      "Quarter": "Y1Q2",
      "Question_5": [{
        "Yes": 30
      }, {
        "No": 5
      }, {
        "Not Sure": 13
      }, {
        "Total": 48
      }],
      "Question_6": [{
        "Yes": 30
      }, {
        "No": 5
      }, {
        "Not Sure": 13
      }, {
        "Total": 48
      }]
    },
    {
      "Quarter": "Y1Q3",
      "Question_5": [{
        "Yes": 32
      }, {
        "No": 1
      }, {
        "Not Sure": 20
      }, {
        "Total": 53
      }],
      "Question_6": [{
        "Yes": 21
      }, {
        "No": 9
      }, {
        "Not Sure": 23
      }, {
        "Total": 53
      }]
    },
    {
      "Quarter": "Y1Q4",
      "Question_5": [{
        "Yes": 79
      }, {
        "No": 7
      }, {
        "Not Sure": 12
      }, {
        "Total": 98
      }],
      "Question_6": [{
        "Yes": 71
      }, {
        "No": 13
      }, {
        "Not Sure": 14
      }, {
        "Total": 98
      }]
    },
    {
      "Quarter": "Y2Q1",
      "Question_5": [{
        "Yes": 88
      }, {
        "No": 5
      }, {
        "Not Sure": 17
      }, {
        "Total": 110
      }],
      "Question_6": [{
        "Yes": 60
      }, {
        "No": 33
      }, {
        "Not Sure": 17
      }, {
        "Total": 110
      }]
    },
    {
      "Quarter": "Y2Q2",
      "Question_5": [{
        "Yes": 94
      }, {
        "No": 9
      }, {
        "Not Sure": 14
      }, {
        "Total": 117
      }],
      "Question_6": [{
        "Yes": 76
      }, {
        "No": 26
      }, {
        "Not Sure": 15
      }, {
        "Total": 117
      }]
    },
    {
      "Quarter": "Y2Q3",
      "Question_5": [{
        "Yes": 38
      }, {
        "No": 5
      }, {
        "Not Sure": 14
      }, {
        "Total": 57
      }],
      "Question_6": [{
        "Yes": 23
      }, {
        "No": 23
      }, {
        "Not Sure": 11
      }, {
        "Total": 57
      }]
    },
    {
      "Quarter": "Y2Q4",
      "Question_5": [{
        "Yes": 48
      }, {
        "No": 2
      }, {
        "Not Sure": 12
      }, {
        "Total": 62
      }],
      "Question_6": [{
        "Yes": 36
      }, {
        "No": 12
      }, {
        "Not Sure": 14
      }, {
        "Total": 62
      }]
    },
    {
      "Quarter": "Y3Q1",
      "Question_5": [{
        "Yes": 90
      }, {
        "No": 2
      }, {
        "Not Sure": 19
      }, {
        "Total": 111
      }],
      "Question_6": [{
        "Yes": 62
      }, {
        "No": 32
      }, {
        "Not Sure": 17
      }, {
        "Total": 111
      }]
    },
    {
      "Quarter": "Y3Q2",
      "Question_5": [{
        "Yes": 55
      }, {
        "No": 3
      }, {
        "Not Sure": 19
      }, {
        "Total": 77
      }],
      "Question_6": [{
        "Yes": 41
      }, {
        "No": 27
      }, {
        "Not Sure": 9
      }, {
        "Total": 77
      }]
    },
    {
      "Quarter": "Y3Q3",
      "Question_5": [{
        "Yes": 43
      }, {
        "No": 2
      }, {
        "Not Sure": 12
      }, {
        "Total": 57
      }],
      "Question_6": [{
        "Yes": 29
      }, {
        "No": 12
      }, {
        "Not Sure": 16
      }, {
        "Total": 57
      }]
    },
    {
      "Quarter": "Y3Q4",
      "Question_5": [{
        "Yes": 40
      }, {
        "No": 5
      }, {
        "Not Sure": 18
      }, {
        "Total": 63
      }],
      "Question_6": [{
        "Yes": 27
      }, {
        "No": 15
      }, {
        "Not Sure": 21
      }, {
        "Total": 63
      }]
    },
    {
      "Quarter": "Y4Q1",
      "Question_5": [{
        "Yes": 77
      }, {
        "No": 8
      }, {
        "Not Sure": 32
      }, {
        "Total": 117
      }],
      "Question_6": [{
        "Yes": 51
      }, {
        "No": 41
      }, {
        "Not Sure": 25
      }, {
        "Total": 117
      }]
    },
    {
      "Quarter": "Current contract to present", // Present: Y4Q1, 8/13/18
      "Question_5": [{
        "Yes": 780
      }, {
        "No": 61
      }, {
        "Not Sure": 230
      }, {
        "Total": 1071
      }],
      "Question_6": [{
        "Yes": 562
      }, {
        "No": 290
      }, {
        "Not Sure": 219
      }, {
        "Total": 1071
      }]
    },
    {
      "Quarter": "Lifetime data to present", // Present: Y4Q1, 8/13/18
      "Question_5": [{
        "Yes": 2409
      }, {
        "No": 297
      }, {
        "Not Sure": 658
      }, {
        "Total": 3364
      }],
      "Question_6": [{
        "Yes": 1743
      }, {
        "No": 1047
      }, {
        "Not Sure": 574
      }, {
        "Total": 3364
      }]
    }
  ]
};

var q5_yes = data.reg_pk_xp.map(function(e) {
  return e.Question_5.map(function(e) {
    return e.Yes;
  });
});

var q5_yes_filtered = q5_yes.map(function(subarray) {
  return subarray.filter(function(val) {
    return val !== undefined;
  });
});
console.log("Question_5 YES only", q5_yes_filtered);

这篇关于使用map()和filter()从嵌套数组映射的新数组中删除未定义的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 21:48