admin管理员组文章数量:1356461
I wanted to ask how to filter my array. In this case my array fills with null object and no null object
[null,{"position":{"lat":50.8999208,"lng":20.6258},"vin":22222}]
normally if null doesn't appear the array looks like this:
`[{"position":{"lat":22.8999208,"lng":22.6258},"vin":11111},{"position":{"lat":50.8999208,"lng":20.6258},"vin":22222}]`
But in this case in my programming scenario the first object is nulled and I have to filter this array or maybe copy no-null objects from this array to another array and then pute or error will occur. How should I do it?
Best regards!
I wanted to ask how to filter my array. In this case my array fills with null object and no null object
[null,{"position":{"lat":50.8999208,"lng":20.6258},"vin":22222}]
normally if null doesn't appear the array looks like this:
`[{"position":{"lat":22.8999208,"lng":22.6258},"vin":11111},{"position":{"lat":50.8999208,"lng":20.6258},"vin":22222}]`
But in this case in my programming scenario the first object is nulled and I have to filter this array or maybe copy no-null objects from this array to another array and then pute or error will occur. How should I do it?
Best regards!
Share Improve this question asked Dec 14, 2019 at 21:31 Snooze SnozeSnooze Snoze 1091 silver badge12 bronze badges 5- How are you getting this array? From an API? – Matt Oestreich Commented Dec 14, 2019 at 21:32
-
array.filter(Boolean)
– trincot Commented Dec 14, 2019 at 21:33 - Yes, from my API – Snooze Snoze Commented Dec 14, 2019 at 21:33
- While it may be an issue with the way you are getting the data, Nina's answer should suffice. – Matt Oestreich Commented Dec 14, 2019 at 21:34
- Does this answer your question? Filter null from an array in javascript – Dexygen Commented Dec 14, 2019 at 21:36
2 Answers
Reset to default 5You could filter the array by checking the data.
var array = [null, { position: { lat: 50.8999208, lng: 20.6258 }, vin: 22222 }],
withoutNull = array.filter(v => v !== null);
console.log(withoutNull);
Or just check truthy values:
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);
本文标签: javascriptHow to filter null from array of objectsStack Overflow
版权声明:本文标题:javascript - How to filter null from array of objects? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743943684a2566020.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论