You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何过滤对象数组,筛选不含子级路径的name属性对象

解决方案

要筛选出name属性不含斜杠的一级条目,可利用数组filter方法结合字符串判断实现,具体如下:

代码实现

const orgUnitArray = [organizationConfig.organizationalUnits];

// 筛选name不含斜杠的对象,保留原二维数组结构
const filteredArray = [
  orgUnitArray[0].filter(item => !item.name.includes('/'))
];

console.log(filteredArray);

代码说明

  • orgUnitArray[0]:取出嵌套数组中的实际对象列表(原输入外层额外包裹了一层数组)
  • filter(item => !item.name.includes('/')):遍历每个对象,仅保留name属性不包含斜杠的条目
  • 最后将筛选结果重新包裹进外层数组,保持和原输入一致的二维结构

输出结果

运行后将得到期望的筛选结果:

[
  [
    { name: 'Security', ignore: undefined },
    { name: 'Infrastructure', ignore: undefined },
    { name: 'Sandbox', ignore: undefined },
    { name: 'Workloads', ignore: undefined },
  ]
]

内容的提问来源于stack exchange,提问作者chris lawrence

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 18:22:13