英文:
How can I filter through an array of objects to select a value that does not have a slack in the output
问题
我正在尝试循环遍历一个对象数组,并且想选择那些具有name: item
但不具有name: item/anotheritem
的项。
示例:
const orgUnitArray = [(organizationConfig.organizationalUnits)]
console.log(orgUnitArray)
console.log
[
[
{ name: 'Security', ignore: undefined },
{ name: 'Infrastructure', ignore: undefined },
{ name: 'Sandbox', ignore: undefined },
{ name: 'Workloads', ignore: undefined },
{ name: 'Workloads/Workload1', ignore: undefined },
{ name: 'Workloads/Workload2', ignore: undefined }
]
]
期望结果:
console.log
[
[
{ name: 'Security', ignore: undefined },
{ name: 'Infrastructure', ignore: undefined },
{ name: 'Sandbox', ignore: undefined },
{ name: 'Workloads', ignore: undefined },
]
]
英文:
I am trying to loop through an array of objects and want to select anything that has name: item
but not name: item/anotheritem
.
Example:
const orgUnitArray = [(organizationConfig.organizationalUnits)]
console.log(orgUnitArray)
console.log
[
[
{ name: 'Security', ignore: undefined },
{ name: 'Infrastructure', ignore: undefined },
{ name: 'Sandbox', ignore: undefined },
{ name: 'Workloads', ignore: undefined },
{ name: 'Workloads/Workload1', ignore: undefined },
{ name: 'Workloads/Workload2', ignore: undefined }
]
]
Expected:
console.log
[
[
{ name: 'Security', ignore: undefined },
{ name: 'Infrastructure', ignore: undefined },
{ name: 'Sandbox', ignore: undefined },
{ name: 'Workloads', ignore: undefined },
]
]
答案1
得分: 2
你可以通过检查项目名称是否包含"/"
并使用filter()
来满足这个条件。
const orgUnitArray = [
{name: "Security", ignore: undefined},
{name: "Infrastructure", ignore: undefined},
{name: "Sandbox", ignore: undefined},
{name: "Workloads", ignore: undefined},
{name: "Workloads/Workload1", ignore: undefined},
{name: "Workloads/Workload2", ignore: undefined}
];
console.log(orgUnitArray.filter(item => !item.name.includes("/")));
英文:
You can simply check whether a item's name includes a "/"
and filter()
by that criteria.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const orgUnitArray = [
{name: "Security", ignore: undefined},
{name: "Infrastructure", ignore: undefined},
{name: "Sandbox", ignore: undefined},
{name: "Workloads", ignore: undefined},
{name: "Workloads/Workload1", ignore: undefined},
{name: "Workloads/Workload2", ignore: undefined}
];
console.log(orgUnitArray.filter(item => !item.name.includes("/")));
<!-- end snippet -->
答案2
得分: 0
我发现这个有帮助,并给了我一些可以使用的东西
const orgUnitArray = (organizationConfig.organizationalUnits).filter(function (orgUnit) {
return orgUnit.name.indexOf('/') === -1;
})
输出:
console.log
[
{ name: 'Security', ignore: undefined },
{ name: 'Infrastructure', ignore: undefined },
{ name: 'Sandbox', ignore: undefined },
{ name: 'Workloads', ignore: undefined }
]
英文:
I found this helped and gave me something to work with
const orgUnitArray = (organizationConfig.organizationalUnits).filter(function (orgUnit) {
return orgUnit.name.indexOf('/') === -1;
})
output:
console.log
[
{ name: 'Security', ignore: undefined },
{ name: 'Infrastructure', ignore: undefined },
{ name: 'Sandbox', ignore: undefined },
{ name: 'Workloads', ignore: undefined }
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论