英文:
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:'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)
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论