从嵌套数组项中删除值反应本机
Remove value from nested array item react native
我有以下数组格式:
[
{
"category": "c8kr0cv012vtr8vm3iqg",
"subcategories": [
"c8kr3d7012vtr8vm3jm0",
"c8kr3d7012vtr8vm3jlg",
"c8kr3d7012vtr8vm3jk0",
"c8kr8h7012vtr8vm3m5g",
"c8tjghovq0ahq4ccf980",
"c8kr3nn012vtr8vm3jog",
"c8kr3nn012vtr8vm3jo0",
"c8kr3nn012vtr8vm3jp0",
]
},
{
"category": "c8kr0cv012vtr8vm3ivg",
"subcategories": [
"c8kr3d7012vtr8vm3jm0",
"c8kr3d7012vtr8vm3jlg",
"c8kr3d7012vtr8vm3jk0",
"c8kr8h7012vtr8vm3m5g",
"c8tjghovq0ahq4ccf980",
"c8kr3nn012vtr8vm3jog",
"c8kr3nn012vtr8vm3jo0",
"c8kr3nn012vtr8vm3jp0",
]
},
{
"category": "c8kr0cv012vtr8vm3is0",
"subcategories": [
"c8kr3d7012vtr8vm3jm0",
"c8kr3d7012vtr8vm3jlg",
"c8kr3d7012vtr8vm3jk0",
"c8kr8h7012vtr8vm3m5g",
"c8kr3nn012vtr8vm3jog",
"c8kr3nn012vtr8vm3jo0",
"c8kr3nn012vtr8vm3jp0",
]
},
{
"category": "c8kr0cv012vtr8vm3ir0",
"subcategories": [
"c8kr3d7012vtr8vm3jm0",
"c8kr3d7012vtr8vm3jlg",
"c8kr3d7012vtr8vm3jk0",
"c8kr8h7012vtr8vm3m5g",
"c8tjghovq0ahq4ccf980",
"c8kr3nn012vtr8vm3jog",
"c8kr3nn012vtr8vm3jo0",
"c8kr3nn012vtr8vm3jp0",
]
}
]
我想从 subcategory
数组中删除值,例如 c8tjghovq0ahq4ccf980
我尝试了以下代码:
tempPayload.some(function (a) {
return a.subcategories.some(function (b, i, bb) {
if (b === "c8tjghovq0ahq4ccf980") {
bb.splice(i, 1);
return true;
}
});
});
但它不起作用我该如何解决这个问题?
尝试使用以下代码:
tempPayload.map((item) => {
const index = item.subcategories.indexOf("c8tjghovq0ahq4ccf980");
if (index > -1) {
item.subcategories.splice(index, 1);
}
});
for(let i=0; i<tempPayload.length; i++){
tempPayload[i].subcategories = tempPayload[i].subcategories.filter(i => i!== "c8tjghovq0ahq4ccf980")
}
console.log(tempPayload)
我有以下数组格式:
[
{
"category": "c8kr0cv012vtr8vm3iqg",
"subcategories": [
"c8kr3d7012vtr8vm3jm0",
"c8kr3d7012vtr8vm3jlg",
"c8kr3d7012vtr8vm3jk0",
"c8kr8h7012vtr8vm3m5g",
"c8tjghovq0ahq4ccf980",
"c8kr3nn012vtr8vm3jog",
"c8kr3nn012vtr8vm3jo0",
"c8kr3nn012vtr8vm3jp0",
]
},
{
"category": "c8kr0cv012vtr8vm3ivg",
"subcategories": [
"c8kr3d7012vtr8vm3jm0",
"c8kr3d7012vtr8vm3jlg",
"c8kr3d7012vtr8vm3jk0",
"c8kr8h7012vtr8vm3m5g",
"c8tjghovq0ahq4ccf980",
"c8kr3nn012vtr8vm3jog",
"c8kr3nn012vtr8vm3jo0",
"c8kr3nn012vtr8vm3jp0",
]
},
{
"category": "c8kr0cv012vtr8vm3is0",
"subcategories": [
"c8kr3d7012vtr8vm3jm0",
"c8kr3d7012vtr8vm3jlg",
"c8kr3d7012vtr8vm3jk0",
"c8kr8h7012vtr8vm3m5g",
"c8kr3nn012vtr8vm3jog",
"c8kr3nn012vtr8vm3jo0",
"c8kr3nn012vtr8vm3jp0",
]
},
{
"category": "c8kr0cv012vtr8vm3ir0",
"subcategories": [
"c8kr3d7012vtr8vm3jm0",
"c8kr3d7012vtr8vm3jlg",
"c8kr3d7012vtr8vm3jk0",
"c8kr8h7012vtr8vm3m5g",
"c8tjghovq0ahq4ccf980",
"c8kr3nn012vtr8vm3jog",
"c8kr3nn012vtr8vm3jo0",
"c8kr3nn012vtr8vm3jp0",
]
}
]
我想从 subcategory
数组中删除值,例如 c8tjghovq0ahq4ccf980
我尝试了以下代码:
tempPayload.some(function (a) {
return a.subcategories.some(function (b, i, bb) {
if (b === "c8tjghovq0ahq4ccf980") {
bb.splice(i, 1);
return true;
}
});
});
但它不起作用我该如何解决这个问题?
尝试使用以下代码:
tempPayload.map((item) => {
const index = item.subcategories.indexOf("c8tjghovq0ahq4ccf980");
if (index > -1) {
item.subcategories.splice(index, 1);
}
});
for(let i=0; i<tempPayload.length; i++){
tempPayload[i].subcategories = tempPayload[i].subcategories.filter(i => i!== "c8tjghovq0ahq4ccf980")
}
console.log(tempPayload)