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

165
Vistas
Quickly toggle a boolean property of objects in an array based on a list of attribute values

I have an array of objects like:

[{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}]

The maximum number of objects in the array is around 7600

I am struggling to find a fast way of flipping the 'selected' property from true to false or vice versa of every object whose id is included in an array of id values like:

['123abc322', '123abc323']

The supplier array of id values could include ALL the object ids and will often be ~2000 ids.

So if the object.id field is in the supplied array I want to swap the object.selected to !object.selected

Any help would be appreciated.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I would recommend converting your array of IDs into a Set. Then, it's a matter of doing one pass over your array and, if the Set contains the element's ID, toggle the selected property.

Note that this method modifies the objects in the array. If you need to not mutate the input, you could use a map.

Mutating the input:

const arr = [{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}];

const ids = new Set(['123abc322', '123abc323']);

arr.forEach(el => {
  if (ids.has(el.id)) {
    el.selected = !el.selected;
  }
});

console.log(arr);

Not mutating the input:

const arr = [{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}];

const ids = new Set(['123abc322', '123abc323']);

const newArr = arr.map(el => {
  if (!ids.has(el.id)) return el;

  return {
    ...el,
    selected: !el.selected
  }
});

console.log(newArr);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do everything with a single line of code, here's a code example:

let array = [{
    type: "marker",
    name: "somename",
    id: "123abc321",
    lat: 123.0,
    lng: 32.0,
    educationRegion: "someregion",
    seDistrict: "sometype",
    sector: "somesector",
    selected: false,
    shownOnMap: true,
    listed: false,
  },
  {
    type: "marker",
    name: "somename",
    id: "123abc322",
    lat: 123.0,
    lng: 32.0,
    educationRegion: "someregion",
    seDistrict: "sometype",
    sector: "somesector",
    selected: false,
    shownOnMap: true,
    listed: false,
  },
  {
    type: "marker",
    name: "somename",
    id: "123abc323",
    lat: 123.0,
    lng: 32.0,
    educationRegion: "someregion",
    seDistrict: "sometype",
    sector: "somesector",
    selected: false,
    shownOnMap: true,
    listed: false,
  },
];

let ids = ['123abc322', '123abc323'];

// .filter() create a new array with all elements that pass the test implemented by the provided function.
// .map() run a function for everyone of the remaining objects and flip the "selected" value.

array.filter(x => ids.includes(x.id)).map(x => x.selected = !x.selected);
console.log(array);

Hope It's what your looking for.

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