使用map()和filter()从嵌套数组映射的新数组中删除未定义的元素

时间:2018-08-15 21:28:37

标签: javascript jquery arrays

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

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

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数组上),但我无法弄清楚。

2 个答案:

答案 0 :(得分:1)

我想你错过了这个:

return e.Yes !== undefined;

在此代码中:

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

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
    },
    {
      Month: "May-2016",
      loOP_Percent_Cert: 95.2,
      loOP_Percent_Exp: 4.8,
      loOP_Percent_None: 0,
      doW_R_Percent_Cert: 80.9,
      doW_R_Percent_Exp: 14.7,
      doW_R_Percent_None: 4.4
    },
    {
      Month: "Aug-2016",
      loOP_Percent_Cert: 95.2,
      loOP_Percent_Exp: 4.8,
      loOP_Percent_None: 0,
      doW_R_Percent_Cert: 83.6,
      doW_R_Percent_Exp: 11.9,
      doW_R_Percent_None: 4.5
    },
    {
      Month: "Nov-2016",
      loOP_Percent_Cert: 91.9,
      loOP_Percent_Exp: 8.1,
      loOP_Percent_None: 0,
      doW_R_Percent_Cert: 89.2,
      doW_R_Percent_Exp: 9.2,
      doW_R_Percent_None: 1.5
    },
    {
      Month: "Feb-2017",
      loOP_Percent_Cert: 95.2,
      loOP_Percent_Exp: 4.8,
      loOP_Percent_None: 0,
      doW_R_Percent_Cert: 90.2,
      doW_R_Percent_Exp: 7.8,
      doW_R_Percent_None: 1.5
    },
    {
      Month: "May-2017",
      loOP_Percent_Cert: 95.2,
      loOP_Percent_Exp: 4.8,
      loOP_Percent_None: 0,
      doW_R_Percent_Cert: 90.2,
      doW_R_Percent_Exp: 7.8,
      doW_R_Percent_None: 1.5
    },
    {
      Month: "Aug-2017",
      loOP_Percent_Cert: 93.4,
      loOP_Percent_Exp: 6.6,
      loOP_Percent_None: 0,
      doW_R_Percent_Cert: 80.7,
      doW_R_Percent_Exp: 17.5,
      doW_R_Percent_None: 1.8
    },
    {
      Month: "Nov-2017",
      loOP_Percent_Cert: 96.7,
      loOP_Percent_Exp: 3.3,
      loOP_Percent_None: 0,
      doW_R_Percent_Cert: 80.7,
      doW_R_Percent_Exp: 17.5,
      doW_R_Percent_None: 1.8
    },
    {
      Month: "Feb-2018",
      loOP_Percent_Cert: 97.0,
      loOP_Percent_Exp: 3.0,
      loOP_Percent_None: 0,
      doW_R_Percent_Cert: 81.0,
      doW_R_Percent_Exp: 17.0,
      doW_R_Percent_None: 2.0
    },
    {
      Month: "May-2018",
      loOP_Percent_Cert: 96.8,
      loOP_Percent_Exp: 3.2,
      loOP_Percent_None: 0,
      doW_R_Percent_Cert: 72.9,
      doW_R_Percent_Exp: 25.4,
      doW_R_Percent_None: 1.7
    }
  ],
  "reg_multi": [{
    "Networks": ["loOP", "NGH", "doW_R", "RES", "MTG", "ERGO", "Other"], //DO NOT CHANGE ORDER
    "Labels": ["None", "1-5", "6-10", "11-25", "More than 25"],
    "None": [846, 1442, 647, 1554, 1589, 1618, 1338],
    "1-5": [487, 231, 697, 126, 101, 69, 229],
    "6-10": [182, 17, 248, 10, 0, 3, 69],
    "11-25": [96, 1, 73, 1, 0, 0, 38],
    "More than 25": [80, 0, 26, 0, 1, 1, 17]
  }],
  "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.filter(function(e) {
    return e.Yes !== undefined;
  });
});
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>

答案 1 :(得分:1)

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

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