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

168
Visualizações
Alternar rápidamente una propiedad booleana de objetos en una matriz en función de una lista de valores de atributo

Tengo una variedad de objetos como:

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

El número máximo de objetos en la matriz es de alrededor de 7600

Estoy luchando por encontrar una forma rápida de cambiar la propiedad 'seleccionada' de verdadero a falso o viceversa de cada objeto cuya identificación se incluye en una matriz de valores de identificación como:

 ['123abc322', '123abc323']

La matriz de valores de identificación del proveedor podría incluir TODAS las identificaciones de objetos y, a menudo, será ~2000 identificaciones.

Entonces, si el campo object.id está en la matriz proporcionada, quiero cambiar el object.selected a !object.selected

Cualquier ayuda sería apreciada.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Recomendaría convertir su matriz de ID en un Set . Luego, se trata de hacer una pasada sobre su matriz y, si el Set contiene la ID del elemento, alternar la propiedad selected .

Tenga en cuenta que este método modifica los objetos en la matriz. Si necesita no mutar la entrada, puede usar un map .

Mutando la entrada:

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

Sin mutar la entrada:

 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

Puede hacer todo con una sola línea de código, aquí hay un ejemplo de código:

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

Espero que sea lo que buscas.

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