[英]Set object property in an array true/false, whether the id matches with any id from another array of objects
[英]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}]
所以这是数组,但是 2 个对象的 id 为“a660f87e-476d-493b-a220-31aff8cf764c”,一个选择了属性 false,另一个选择了 true,所以如果为 false,我希望删除具有该 id 的对象,所以我需要最终结果像这样。
[{id: '1e8284a5-6ba3-48c0-b8ac-f6ffae55b44e', selected: true}]
如果数组是这样的
[{id: 'a660f87e-476d-493b-a220-31aff8cf764c', selected: true}
{id: '1e8284a5-6ba3-48c0-b8ac-f6ffae55b44e', selected: true}
{id: 'a660f87e-476d-493b-a220-31aff8cf764c', selected: true}]
我想要结果为
[{id: 'a660f87e-476d-493b-a220-31aff8cf764c', selected: true}
{id: '1e8284a5-6ba3-48c0-b8ac-f6ffae55b44e', selected: true}
]`
任何人都可以提出解决方案。
谢谢
如果您只想选择那些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);
您可以像if
条件一样为您提供检查以进行filter
所以你的问题中有两个行动项目。
selected
为 true 时才获取项目id
已在新数组中可用,则不需要再次包含(就像您想要唯一数组一样)。你可以像下面这样实现它。
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);
所以基本上你想从数组中删除带有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;
}
在您的问题中没有逗号 (,) b/w 元素,因此请编辑您的问题。
在不使用任何 3rd 方库的情况下,我所做的是构建一个字典,其中每个键都是您的id
,每个值是{count: <the number of ids>, isSelected: <are all ids selected>}
。
然后当我迭代数组时,我有 3 个案例 -
id
- 留在结果中。id
但被选中为false
- 全部删除。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.