[英]remove object property in nested json by map javascript
I'm trying to remove a property colorIds from my total json response.我正在尝试从我的总 json 响应中删除属性 colorIds 。
const catalog = (materials) => (finishes) => (colors) =>
materials.map((material) => ({
...material,
finishes: finishes
.filter((finish) => {
const materialId = finish.materialId;
delete finish.materialId;
return materialId == material.id;
})
.map((finish) => ({
...finish,
colors: colors.filter((color, i) => {
const colorIds = finish.colorIds[i];
return colorIds.includes(color._id);
}),
})),
}));
each finish has materialId
and colorIds
which we don't want so I'm currently deleting the finish.materialId after it's been filtered.每个完成都有我们不想要的materialId
和colorIds
,所以我目前正在删除过滤后的 finish.materialId。 I'm trying to do the same for the colorIds
as well but it seems that it's needed in the colors
inner filter()
.我也在尝试对colorIds
做同样的事情,但似乎colors
内部filter()
需要它。
I was wondering if there's a way to over come I tried adding subsequent map
s to no avail.我想知道是否有办法克服我尝试添加后续的map
无济于事。
const removeKeys = (obj, keys) => obj !== Object(obj)
? obj
: Array.isArray(obj)
? obj.map((item) => removeKeys(item, keys))
: Object.keys(obj)
.filter((k) => !keys.includes(k))
.reduce(
(acc, x) => Object.assign(acc, { [x]: removeKeys(obj[x], keys) }),
{}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.