[英]in mongodb, whats the easiest way to get an array of a single field from the documents
[英]Whats the easiest way to remove null values from an array with a loop (Js)
如果我有一個包含三個文本值和兩個空值的數組。 如何用循環將其刪除。 是否可以使用拼接來執行此操作。
arrayVal[0] = null
arrayVal[1] = "Some text"
arrayVal[2] = null
arrayVal[3] = "More text"
arrayVal[4] = "Text Again"
我要實現以下目標:
arrayVal[0] = "Some Text"
arrayVal[1] = "More text"
arrayVal[2] = "Text Again"
這是一種方法:
arrayVal.filter(Boolean);
請注意,布爾值將刪除任何偽造的值,包括零,空字符串,空值,未定義。
這也可以通過for
循環來實現。
function removeNull() {
var arrayVal, newArrayVal, j;
for (var i = 0; i < arrayVal.length; i++) {
j = 0;
if (arrayVal[i]) {
newArrayVal[j] = arrayVal[i];
j++;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.