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

162
Visualizações
Encontrar valor dentro de un objeto, eso está dentro de una matriz

Tengo dos matrices que quiero comparar. El estado de cada objeto puede ser verdadero, falso o nulo según lo que haya seleccionado el usuario.
Luego quiero comparar las dos matrices y enviar el nombre de la actividad a una matriz separada si coinciden.
Si compara el primero, por ejemplo, que sería "gratis", y el usuario ha marcado esto, haciendo {gratis: verdadero}, entonces esta actividad ("Ir al cine") no se insertará en la nueva matriz.

 //user input filtersChecked: [ { free: null }, { indoors: null }, { city: null }, { winter: null }, { physical: null }, ], //compared against: name: "Go to the cinema", filters: [ { free: false }, { indoors: true }, { city: true }, { winter: null }, { physical: false }, ],

Con lo que estoy luchando es comparando el "índice" del estado de los objetos si eso tiene algún sentido ...
Mi intento se ve así:

 function checkFilter(index, isChecked) { if (isChecked) { model.input.filtersChecked[index] = true; } else if (!isChecked) { model.input.filtersChecked[index] = false; } } function suggestActivities() { let userFilters = model.input.filtersChecked; let activityFilters; let tempArray = []; for (let i = 0; i < model.data.activities.length; i++) { activityFilters = model.data.activities[i].filters; for (let j = 0; j < userFilters.length; j++) { if (userFilters[j] === activityFilters[j]) { tempArray.push(model.data.activities[i].name); console.log(tempArray); continue; } } } console.log(tempArray); }

Espero que sea lo suficientemente detallado. He estado atascado en esto durante demasiado tiempo ahora.
También vale la pena mencionar que me gustaría que fuera JavaScript estándar, ya que esta es una tarea escolar. ¡Así que no jQuery por favor!

¡Gracias de antemano!

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

0

Aquí hay una muestra de cómo podrías hacer esta comparación, con los subproblemas en los que podrías dividirla. Úselo como una plantilla sobre cómo resolver el problema de su tarea. Avísame si te ayuda.

 const userInput = [ { free: null }, { indoors: null }, { city: null }, { winter: null }, { physical: null }, ] const compared = { name: "Go to movies" filters: [ { free: false }, { indoors: true }, { city: true }, { winter: null }, { physical: false }, ] } const tempArr = [] function compareObjects (userInput, compared) { //loop through userInput filters for (let i=0; i<userInput.length; i++){ //grab key from current index of array (like "free", "indoors", etc) let key = Object.keys(userInput[i])[0] //compare stored filters with user filters //if not equal, we can stop searching through the filters entirely and exit the function, // since if one of them doesnt match, we wont push at all //Note, I can reuse the userInput key for the compared keys, because both arrays are of equal length. //If they weren't the same length, the keys wouldn't match, and you would have to sort the array first //so the index order and keys matched for both inputs. if(userInput[i][key] !== compared.filters[i][key]) return } // if we made it through this loop without exiting the function, we can // push the name since it means they all matched tempArr.push(compared.name) } //call the function and pass in variables compareObjects(userInput, compared)
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