【发布时间】:2021-06-22 21:25:52
【问题描述】:
我有以下数组,下面有更多详细信息
[
{
"__typename": "Decor",
"itemNum": 1,
"purchaseDate": "2021-04-04",
"description": "fdsf",
"alterations": true,
"cost": 44,
"pieces": 3,
"category": "Curation",
"purchaser": "fdsfa",
"image": "df",
"_id": "293164620554699277",
"visible": false
},
{
"__typename": "Decor",
"itemNum": 2,
"purchaseDate": "2021-04-02",
"description": "Blue Jeansgfg",
"alterations": true,
"cost": 33,
"pieces": 33,
"category": "Curation",
"purchaser": "fdsf",
"image": "fds",
"_id": "293164663883956749",
"visible": false
},
{
"__typename": "Decor",
"itemNum": 3,
"purchaseDate": "2021-03-24",
"description": "fdsfsa",
"alterations": true,
"cost": 3,
"pieces": 4,
"category": "Curation",
"purchaser": "fdsfa",
"image": "fds",
"visible": false
},
{
"__typename": "Decor",
"itemNum": 4,
"purchaseDate": "2021-03-12",
"description": "Pair of Michaels SHoes",
"alterations": true,
"cost": 5,
"pieces": 2,
"category": "Curation",
"purchaser": "fdsfa",
"image": "fdf",
"visible": true
}
]
我正在映射,然后过滤,然后映射,它主要完成我的预期,但结果有空数组值,我只想将它们一起删除。
const massaged = decorData?.findUserByID?.decor?.data?.map((item) => {
var col = Object.values(item);
return col
.filter(function (hype) {
console.log(hype);
if (item.visible === false) {
return false;
}
return true;
})
.map((colItem, i) => {
return { [`col${i}`]: colItem };
});
});
这是结果,我想删除所有 [] 数组值。我不太清楚为什么他们只是返回空而不是一起删除
[
[], //I WANT ALL THESE EMPTY ARRAYS REMOVED
[], //I WANT ALL THESE EMPTY ARRAYS REMOVED
[], //I WANT ALL THESE EMPTY ARRAYS REMOVED
[
{
"col0": "Decor"
},
{
"col1": 4
},
{
"col2": "2021-03-12"
},
{
"col3": "Pair of Michaels SHoes"
},
{
"col4": true
},
{
"col5": 5
},
{
"col6": 2
},
{
"col7": "Curation"
},
{
"col8": "fdsfa"
},
{
"col9": "fds.png"
},
{
"col10": "294069217411465741"
},
{
"col11": true
}
]
]
提前谢谢
【问题讨论】:
标签: javascript arrays loops array.prototype.map