如何在对象数组中进行筛选,以选择输出中没有松弛的值?

huangapple go评论70阅读模式
英文:

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: &quot;Security&quot;, ignore: undefined},
  {name: &quot;Infrastructure&quot;, ignore: undefined},
  {name: &quot;Sandbox&quot;, ignore: undefined},
  {name: &quot;Workloads&quot;, ignore: undefined},
  {name: &quot;Workloads/Workload1&quot;, ignore: undefined},
  {name: &quot;Workloads/Workload2&quot;, ignore: undefined}
];

console.log(orgUnitArray.filter(item =&gt; !item.name.includes(&quot;/&quot;)));

<!-- 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(&#39;/&#39;) === -1;
})

output:

 console.log
 [
  { name: &#39;Security&#39;, ignore: undefined },
  { name: &#39;Infrastructure&#39;, ignore: undefined },
  { name: &#39;Sandbox&#39;, ignore: undefined },
  { name: &#39;Workloads&#39;, ignore: undefined }
  ]

huangapple
  • 本文由 发表于 2023年8月9日 01:07:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定