Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda