我有一组复选框-用户在其上打勾。复选框会传递一些数据,一个ID和一个名称,我以后需要进行排序。因为即使两个对象包含相同的值,它们也不相等,所以我不能使用Array.includes。
以下是数据示例
[
{
"id": 9,
"name": "age_group_ids"
},
{
"id": 9,
"name": "age_group_ids"
},
{
"id": 9,
"name": "earnings_group_ids"
},
{
"id": 3,
"name": "earnings_group_ids"
},
]
这是当前函数(如果项目不是对象,则可以使用
const submitFilterDetails = (value) => {
return async (dispatch,getState) => {
const currentArray = (getState().filter.filtersArray);
if(!currentArray.includes(value)) {
dispatch(addToFiltersArray(value))
} else {
dispatch(removeFromFiltersArray(value));
}
}
}
如何对它进行排序,这样我只有唯一的值
答案 0 :(得分:0)
const src = [
{
"id": 9,
"name": "age_group_ids"
},
{
"id": 9,
"name": "age_group_ids"
},
{
"id": 1,
"name": "earnings_group_ids"
},
]
const out = src.reduce((acc, curr) => acc.some(i => i.id === curr.id) ? acc : acc.concat([curr]) , [])
// the `out` variable has 2 unique items
答案 1 :(得分:0)
您可以使用find:
YourArray.find(obj => obj.id == value.id && obj.name == value.name);