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

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

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

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