gpt4 book ai didi

javascript - 迭代数组并仅删除 id

转载 作者:行者123 更新时间:2023-12-01 02:19:44 25 4
gpt4 key购买 nike

{
"productGroupVariantss": [
{
"id": 1378,
"name": "No oF Poles",
"variantsAttributeses": [
{
"id": 391,
"variantsId": null,
"variantsValue": "1p"
},
{
"id": 392,
"variantsId": null,
"variantsValue": "2p"
},
{
"id": 393,
"variantsId": null,
"variantsValue": "5p"
},
{
"id": 394,
"variantsId": null,
"variantsValue": "4p"
},
{
"id": 395,
"variantsId": null,
"variantsValue": "6p"
}
]
}
]
}

在这个json对象中,我需要迭代并仅省略数组productGroupVariantss和variantsAttributeses中的id。现在我尝试映射数组并忽略它没有返回相同的 json ??

CloneData.productGroupVariantss = []
var Variant = _.map(model.productGroupVariantss, function(object) {
_.omit(object, ['id']);
_.map(object.variantsAttributeses,function(data){
var p = _.pull(object.variantsAttributeses, 'id');
var s= _.omit(data, ['id']);
CloneData.productGroupVariantss.push({
name:p,
calue:s
})
})
});

我的预期输出是

{
"productGroupVariantss": [
{
"name": "No oF Poles",
"variantsAttributeses": [
{

"variantsId": null,
"variantsValue": "1p"
},
{

"variantsId": null,
"variantsValue": "2p"
},
{

"variantsId": null,
"variantsValue": "5p"
},
{

"variantsId": null,
"variantsValue": "4p"
},
{

"variantsId": null,
"variantsValue": "6p"
}
]
}
]
}

最佳答案

您可以尝试这个,这是一个非常简单的解决方案。

productGroupVariantss.forEach(function(o, i) {
if (o.id) {
delete o.id;
}
if (o.variantsAttributeses) {
o.variantsAttributeses.forEach(function(child) {
if (child.id) {
delete child.id;
}
});
}
});

console.log( productGroupVariantss );

关于javascript - 迭代数组并仅删除 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49288220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com