Angular:如何使用多个条件筛选对象数组

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

Angular: How can I filter an object-Array with multiple conditions

问题

有一个对象数组,像这样:

myObjectArray = [
 {name:'John', label:'ab', isActive: false},
 {name:'Sara', label:'bb', isActive: false},
 {name:'John', label:'cb', isActive: false},
 {name:'Nicole', label:'eb', isActive: true},
 {name:'Nicole', label:'eb', isActive: true},
 {name:'Nicole', label:'eb', isActive: true},
 {name:'Nicole', label:'eb', isActive: true},
 {name:'Sara', label:'fb', isActive: false},
 {name:'Thomas', label:'gb', isActive: false},
 {name:'Holly', label:'gb', isActive: true},
 {name:'Nicole', label:'hb', isActive: true},
 {name:'Nicole', label:'eb', isActive: true},
 {name:'Sara', label:'ib', isActive: false}
]

如何按照这样的方式筛选数组,以获得这个数组:

myObjectArray = [
 {name:'John', label:'ab', isActive: false},
 {name:'Sara', label:'bb', isActive: false},
 {name:'Nicole', label:'eb', isActive: true},
 {name:'Thomas', label:'gb', isActive: false},
 {name:'Holly', label:'gb', isActive: true},
]

如何使用“find”在第一个数组中过滤重复项,以获得第二个数组。Nicole在数组中出现多次。但必须保留,因为isActive == true。谢谢。

更新:
我需要第一个数组中所有仅一次的对象,当isAktive == false。但我还需要所有isActive == true的对象,无论它们出现多少次。谢谢。

英文:

I have an object-array like this:

myObjectArray = [
 {name:'John', label:'ab', isActive: false},
 {name:'Sara', label:'bb', isActive: false},
 {name:'John', label:'cb', isActive: false},
 {name:'Nicole', label:'eb', isActive: true},
 {name:'Nicole', label:'eb', isActive: true},
 {name:'Nicole', label:'eb', isActive: true},
 {name:'Nicole', label:'eb', isActive: true},
 {name:'Sara', label:'fb', isActive: false},
 {name:'Thomas', label:'gb', isActive: false},
 {name:'Holly', label:'gb', isActive: true},
 {name:'Nicole', label:'hb', isActive: true},
 {name:'Nicole', label:'eb', isActive: true},
 {name:'Sara', label:'ib', isActive: false}
]

How can i filter this array like this to get this array:

myObjectArray = [
 {name:'John', label:'ab', isActive: false},
 {name:'Sara', label:'bb', isActive: false},
 {name:'Nicole', label:'eb', isActive: true},
 {name:'Thomas', label:'gb', isActive: false},
 {name:'Holly', label:'gb', isActive: true},
]

How can I filter duplicates with "find" in first array to get the scond array. Nicole occurs multiple times in the array. However, it must be preserved because isActive == true. Thanks.

Update:
I need all objects in the first array only once when isAktive == false. But I also need all objects that are isActive == true, no matter how many times they occur. Thanks.

答案1

得分: 0

你可以在对象数组上使用筛选器,条件是:

  • 对于 isActive = false,通过比较它们的名称来删除重复项

或者

  • 对于 isActive = true,允许重复项
const result = [
  { name: 'John', label: 'ab', isActive: false },
  { name: 'Sara', label: 'bb', isActive: false },
  { name: 'John', label: 'cb', isActive: false },
  { name: 'Nicole', label: 'eb', isActive: true },
  { name: 'Sara', label: 'fb', isActive: false },
  { name: 'Thomas', label: 'gb', isActive: false },
  { name: 'Nicole', label: 'hb', isActive: true },
  { name: 'Sara', label: 'ib', isActive: false }
].filter((value, index, self) =>
  index === self.findIndex((t) => (
    !t.isActive && t.name === value.name
  )) || value.isActive
)

console.log(result)
英文:

You can use filter on your object array with condition of

remove duplicates by comparing their name for isActive = false

or

allow duplicates for isActive = true

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const result = [
 {name:&#39;John&#39;, label:&#39;ab&#39;, isActive: false},
 {name:&#39;Sara&#39;, label:&#39;bb&#39;, isActive: false},
 {name:&#39;John&#39;, label:&#39;cb&#39;, isActive: false},
 {name:&#39;Nicole&#39;, label:&#39;eb&#39;, isActive: true},
 {name:&#39;Sara&#39;, label:&#39;fb&#39;, isActive: false},
 {name:&#39;Thomas&#39;, label:&#39;gb&#39;, isActive: false},
 {name:&#39;Nicole&#39;, label:&#39;hb&#39;, isActive: true},
 {name:&#39;Sara&#39;, label:&#39;ib&#39;, isActive: false}
].filter((value, index, self) =&gt;
  index === self.findIndex((t) =&gt; (
    !t.isActive &amp;&amp; t.name === value.name
  )) || value.isActive
)

console.log(result)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月1日 16:36:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75601258.html
匿名

发表评论

匿名网友

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

确定