Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
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.

[{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.

 [{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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

All you need is Array.reduce with 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.

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

about 4 years ago · Juan Pablo Isaza Report

0

So two action items in your questions.

  1. Get the items only if selected is true
  2. If the selected id already available in new array then not required to include again(Like you want unique array).

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);

about 4 years ago · Juan Pablo Isaza Report

0

So basically you want to remove object with selected: false from the array, right?

Maybe you can use a loop to remove the object with 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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!