React - TypeScript -多维对象-需要支持



我有一个像下面这样的对象。我只需要删除数组对象的特定部分。

我需要删除这个基于一些条件。请提供一些建议。

我们可以使用单行数组反结构选项或任何其他解决方案吗?

这里唯一的标识符是"menuKey"; "sidebarRewards">

需要移除的部分:

{
"menuKey": "sidebarRewards",
"label": "Smart Rewards",
"iconSetName": "narrow",
"description": "",
"url": "example.com/account/sidebar/index.tmpl#rewards"
}

原始对象:

{
"profile-welcome": {
"type": "user-profile-welcome",
"params": {
"welcomeBack": "Welcome Back!"
}
},
"sidebar-tier": {
"type": "user-sidebar-tier",
"params": {
"rewardsLink": {
"label": "REWARDS",
"url": "/smartrewards"
}
}
},
"account-links": {
"type": "account-links",
"params": {
"menu": [
{
"menuKey": "sidebarAccount",
"label": "My Account",
"iconSetName": "narrow",
"description": "",
"url": "example.com/account/index.tmpl"
},
{
"menuKey": "sidebarSettings",
"label": "Account Settings",
"iconSetName": "narrow",
"description": "",
"url": "example.com/account/profile_preferences.tmpl"
},
{
"menuKey": "sidebarRewards",
"label": "Smart Rewards",
"iconSetName": "narrow",
"description": "",
"url": "example.com/account/sidebar/index.tmpl#rewards"
},
{
"menuKey": "sidebarSignOut",
"label": "Sign Out ",
"iconSetName": "narrow",
"description": "",
"url": "example.com/account/signin.tmpl?_SUBMIT=signout"
}
]
}
},
"showcase": {
"type": "showcase",
"params": {
"brandServices": {
"header": "SKIN SERVICES",
"infoLink": {
"label": "",
"url": ""
},
"list": [
{
"showCaseKey": "firstBrandShowcase",
"title": "Skin Services.",
"teaser": "Our team of Consultants is at your fingertips for tips and advice.",
"label": "See All Skin Services",
"url": "/services"
},
{
"showCaseKey": "secondBrandShowcase",
"title": "Try on makeup.",
"teaser": "Find your perfect shades and use your phone to try them on.",
"label": "Select Makeup to Try On",
"url": "/clinicalreality"
}
]
}
}
}
}

您总是可以使用数组filter方法。例如:

if (condition) {
account-links.menu = account-links.menu.filter(menu => {
return menu.menuKey !== "sidebarAccount"
}
}

这将使用基于您选择检查的任何标准的过滤版本覆盖account-links.menu

相关内容

最新更新