javascript - 删除嵌套数组中的空对象
问题描述
我有一个数组
const exampleArray = [
{
id: 1,
name: test
},
{
id: 1,
name: test
},
{
},
]
现在该数组exampleArray
位于另一个对象数组中,所以
const exampleNest = [{
property1: {},
property2: [],
exampleArray: [{...}],
}]
我需要删除里面的空对象exampleArray
并返回数组的其余部分。
我已经尝试过使用过滤器
const noEmptyObjects = exampleNest.filter(({ exampleArray }) =>
exampleArray.filter(attachment => attachment !== {}),
);
我也尝试过Object.keys(attachment).length !== 0
,attachment !== {}
但我继续收到带有空项目的数组。
解决方案
您可以使用简单的https://stackoverflow.com/a/32108184/3932166来检查空对象和数组。
exampleArray.filter(attachment => (condition from above link));
推荐阅读
- android - 升级到 Android Studio 北极狐 Beta 4 后无法构建
- apache2 - 在生产服务器中打开 /sitemap.xml 失败(404 页面)
- sql-server - SQL Server 中是否有一种优雅的方法可以在另一个字符串中的任何位置插入一个字符串 - 包括结尾?
- ios - CocoaPods:如何将纯 swift 静态库预编译的二进制文件包装到 pod 中?
- linux - 大的发送套接字缓冲区大小导致 UDP 数据包丢失
- java - 从 Kotlin 理解这段代码
- firebase - Flutter中如何验证用户输入数据是否存在于Firestore文档中?
- apache-spark - 如何从 spark sql databricks 中的临时视图或等效视图中删除?
- animation - 如何使用 ArcHandles 和 Handles 旋转/移动包含 Motor6D 的对象?
- python - 为什么我在 Python3 中收到此错误:IndexError: list index out of range