Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda