[英]How to filter null from array of objects?
我想問一下如何過濾我的數組。 在這種情況下,我的數組填充了空對象並且沒有空對象
[null,{"position":{"lat":50.8999208,"lng":20.6258},"vin":22222}]
通常,如果沒有出現 null,則數組如下所示:
`[{"position":{"lat":22.8999208,"lng":22.6258},"vin":11111},{"position":{"lat":50.8999208,"lng":20.6258},"vin":22222}]`
但在這種情況下,在我的編程場景中,第一個對象為空,我必須過濾這個數組,或者可能將非空對象從這個數組復制到另一個數組,然后計算或錯誤將發生。 我該怎么做?
此致!
或者只是檢查真值:
truthyValues = array.filter(v => v);
let array = [null,{"position":{"lat":50.8999208,"lng":20.6258},"vin":22222}], truthyValues = array.filter(v => v); console.log(truthyValues);
您可以通過檢查數據來過濾數組。
var array = [null, { position: { lat: 50.8999208, lng: 20.6258 }, vin: 22222 }], withoutNull = array.filter(v => v !== null); console.log(withoutNull);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.