[英]There is 2 array of object with same id, but one object property is true and other property is false, how to remove both if property is false
I have array of object, there is 2 array of object with same id, but one object property is true and other property is false, how to remove both if property is false.我有对象数组,有 2 个具有相同 id 的对象数组,但一个对象属性为真,另一个属性为假,如果属性为假,如何删除两者。
[{id: 'a660f87e-476d-493b-a220-31aff8cf764c', selected: true}
{id: '1e8284a5-6ba3-48c0-b8ac-f6ffae55b44e', selected: true}
{id: 'a660f87e-476d-493b-a220-31aff8cf764c', selected: false}]
So this is the array but 2 object have id "a660f87e-476d-493b-a220-31aff8cf764c" and one has selected property false and another one true, so if there is false I want object with that id should remove so I need final result like this.所以这是数组,但是 2 个对象的 id 为“a660f87e-476d-493b-a220-31aff8cf764c”,一个选择了属性 false,另一个选择了 true,所以如果为 false,我希望删除具有该 id 的对象,所以我需要最终结果像这样。
[{id: '1e8284a5-6ba3-48c0-b8ac-f6ffae55b44e', selected: true}]
If array is like this如果数组是这样的
[{id: 'a660f87e-476d-493b-a220-31aff8cf764c', selected: true}
{id: '1e8284a5-6ba3-48c0-b8ac-f6ffae55b44e', selected: true}
{id: 'a660f87e-476d-493b-a220-31aff8cf764c', selected: true}]
I want result as我想要结果为
[{id: 'a660f87e-476d-493b-a220-31aff8cf764c', selected: true}
{id: '1e8284a5-6ba3-48c0-b8ac-f6ffae55b44e', selected: true}
]`
Can anyone suggest solution .任何人都可以提出解决方案。
Thanks谢谢
All you need is Array.reduce
with Array.filter
您只需要
Array.reduce
和Array.filter
If you want to select only those object whose selected
property is true, irrespective of what value id holds then can try below snippet.如果您只想选择那些
selected
属性为 true 的对象,无论 id 持有什么值,都可以尝试下面的代码段。
let arr = [ {id: 'a660f87e-476d-493b-a220-31aff8cf764c', selected: true}, {id: '1e8284a5-6ba3-48c0-b8ac-f6ffae55b44e', selected: true}, {id: '1e8284a5-6ba3-48c0-b8ac-f6ffae55b44f', selected: false}, {id: '1e8284a5-6ba3-48c0-b8ac-f6ffae55b44e', selected: false}, {id: 'a660f87e-476d-493b-a220-31aff8cf764c', selected: true} ] arr = Object.values(arr.reduce( (a, b) => { a[b.id] = a[b.id] ? a[b.id] : b ; return a; }, {})).filter(a => a.selected === true) console.log(arr);
You can provide you check just like if
condition to filter
您可以像
if
条件一样为您提供检查以进行filter
So two action items in your questions.所以你的问题中有两个行动项目。
selected
is trueselected
为 true 时才获取项目id
already available in new array then not required to include again(Like you want unique array).id
已在新数组中可用,则不需要再次包含(就像您想要唯一数组一样)。 you can achieve it like below.你可以像下面这样实现它。
let tmp = [{id: '1', selected: true}, {id: '2', selected: true}, {id: '1', selected: true}]; var newArr = []; var tmp1 = tmp.map((itm) => { var t = newArr.map(a => a.id); if(t.indexOf(itm.id) == -1 && itm.selected) { newArr.push(itm); } }); console.log(newArr);
So basically you want to remove object with selected: false
from the array, right?所以基本上你想从数组中删除带有
selected: false
的对象,对吧?
Maybe you can use a loop to remove the object with selected: false
.也许您可以使用循环来删除具有
selected: false
的对象。
let _array = [{
id: 'a660f87e-476d-493b-a220-31aff8cf764c',
selected: true
},
{
id: '1e8284a5-6ba3-48c0-b8ac-f6ffae55b44e',
selected: true
},
{
id: 'a660f87e-476d-493b-a220-31aff8cf764c',
selected: false
}
];
let temp_array = [];
_array.forEach((object, index) => {
if (object.selected == false && hasDuplicate(object.id) == true) {
_array.splice(index, 1);
}
});
function hasDuplicate(id) {
// function for finding if the element has a duplicate.
_array.forEach(object => {
temp_array.push(object.id)
}); // A temporary array for storing all the ids.
var indexes = [],
i = -1;
while ((i = temp_array.indexOf(id, i + 1)) != -1) {
indexes.push(i);
} // Finds indexes of all object with same id
if (indexes.length > 1) {
return true;
} // if has more than 1 index it returns true (hasDuplicate = true)
return false;
}
In your question there is no comma (,) b/w the elements so please edit your question.在您的问题中没有逗号 (,) b/w 元素,因此请编辑您的问题。
Without using any 3rd party library, what I did was to build a dictionary where each key is your id
and each value is {count: <the number of ids>, isSelected: <are all ids selected>}
.在不使用任何 3rd 方库的情况下,我所做的是构建一个字典,其中每个键都是您的
id
,每个值是{count: <the number of ids>, isSelected: <are all ids selected>}
。
Then when I iterate the array I have 3 cases -然后当我迭代数组时,我有 3 个案例 -
id
- stay in the result.id
- 留在结果中。id
but is selected is false
- remove all.id
但被选中为false
- 全部删除。id
but is selected is true
- remove until we will have count === 1
, which will fall to the first case. id
but selected is true
- remove until we will have count === 1
,这将属于第一种情况。 function extract(array) { let ids = {}; array.forEach(item => { if (!ids[item.id]) ids[item.id] = { count: 1, isSelected: item.selected }; else { ids[item.id].count++; ids[item.id].isSelected = ids[item.id].isSelected && item.selected; } }); return array.filter(item => { if (ids[item.id].count === 1) return true; else if (!ids[item.id].isSelected) return false; else ids[item.id].count--; return false; }) } const arr1 = [{ id: 'a660f87e-476d-493b-a220-31aff8cf764c', selected: true }, { id: '1e8284a5-6ba3-48c0-b8ac-f6ffae55b44e', selected: true }, { id: 'a660f87e-476d-493b-a220-31aff8cf764c', selected: false } ]; const arr2 = [{ id: 'a660f87e-476d-493b-a220-31aff8cf764c', selected: true }, { id: '1e8284a5-6ba3-48c0-b8ac-f6ffae55b44e', selected: true }, { id: 'a660f87e-476d-493b-a220-31aff8cf764c', selected: true } ]; console.log(extract(arr1)); console.log(extract(arr2));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.