简体   繁体   English

有2个具有相同id的对象数组,但一个对象属性为真,另一个属性为假,如果属性为假,如何删除两者

[英]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.reduceArray.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.所以你的问题中有两个行动项目。

  1. Get the items only if selected is true仅当selected为 true 时才获取项目
  2. If the selected 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 个案例 -

  1. only single id - stay in the result.只有一个id - 留在结果中。
  2. more than 1 id but is selected is false - remove all.超过 1 个id但被选中为false - 全部删除。
  3. more than 1 id but is selected is true - remove until we will have count === 1 , which will fall to the first case. more than 1 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.

相关问题 在数组中设置对象属性 true/false,无论 id 是否与另一个对象数组中的任何 id 匹配 - Set object property in an array true/false, whether the id matches with any id from another array of objects 如果某些属性为false,则对对象进行角度过滤数组? - Angular filtering array of object if certain property is false? 检查对象属性值是否为false - Check if object property value is false 如何将对象属性复制到其他对象,然后从数组中删除该对象? - How to copy object property to other object and then remove this object from array? Keycloak 对象属性“已验证”在应该为真时返回假 - Keycloak object property "authenticated" returns false when should be true 在使用工厂函数创建的对象的属性中切换真或假 - toggle true or false in an property of an object created using factory function JavaScript对象-查找一个属性-&gt;更改为true,将其他所有属性更改为false - JavaScript Object - Find one property -> change to true and change all others to false 删除数组中 object 的属性 - Remove property for object in array 检测数组中的对象是否具有相同的属性,但其他属性与其他对象不同 - Detect if object in array has one same property but different other property to other objects AngularJS ng-show是否将对象数组中的属性设置为false - AngularJS ng-show if a property within an object array is set to false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM