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

250
Visualizações
Comparing values from an object and an array to get output from the array

What is the best way to parse common data from an object and an array to get a specific value from the current data.

In this case I am looking at current_data[3] and dog_database.vets to see the output of current_data[4].

current_data = ['true', 'visiting-today', 'DVM-Wiessman','J-001'];


var dog_database = [
  {"dog_name": "Joey",    "chip_id": "001", "breed": "mixed",  "vets":['DVM-Wiessman','DVM-White']},
  {"dog_name": "Max",     "chip_id": "002", "breed": "beagle", "vets":'DVM-Wiel'},
  {"dog_name": "Izzy",    "chip_id": "003", "breed": "mixed",  "vets":'DVM-James'},
  {"dog_name": "Frankie", "chip_id": "004", "breed": "terrier","vets":'DVM-Smith'},
  {"dog_name": "Star",    "chip_id": "005", "breed": "husky",  "vets":['DVM-Johns','DVM-Pearson']},
  {"dog_name": "Goku",    "chip_id": "006", "breed": "lab",    "vets":'undecided'},
];


//How I thought about approaching it in psuedocode
if(current_data[3] == dog_database.vets || dog_database.vets.includes(current_data[3])) {
   console.log(current_data[4])
}
else{  
   console.log('does not exist');

}

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

0

Not sure there is a "best" way because it very much depends on the shape of your data. But what you have isn't far off.

It seems like a small helper function might make this more extensible. Something like

var current_data = ['true', 'visiting-today', 'DVM-Wiessman','J-001'];

const VET_INDEX = 2
const RESULT_INDEX = 3

var dog_database = [
  {"dog_name": "Joey",    "chip_id": "001", "breed": "mixed",  "vets":['DVM-Wiessman','DVM-White']},
  {"dog_name": "Max",     "chip_id": "002", "breed": "beagle", "vets":'DVM-Wiel'},
  {"dog_name": "Izzy",    "chip_id": "003", "breed": "mixed",  "vets":'DVM-James'},
  {"dog_name": "Frankie", "chip_id": "004", "breed": "terrier","vets":'DVM-Smith'},
  {"dog_name": "Star",    "chip_id": "005", "breed": "husky",  "vets":['DVM-Johns','DVM-Pearson']},
  {"dog_name": "Goku",    "chip_id": "006", "breed": "lab",    "vets":'undecided'},
];


function findByVet(vetName) {
  return dog_database.find((row) => row.vets == vetName || row.vets.includes(vetName))
}

if (findByVet(current_data[VET_INDEX])) {
   console.log( current_data[RESULT_INDEX] )
} else {
   console.log( "does not exist" )
}

As you can see I added a couple constants to explain/show what each field in current_data is supposed to represent. Not sure that RESULT_INDEX is really the right name but I'm sure you could make that more informative/better named.

Not sure how much control you have over the current_data, but if that were an object, more like

var current_data = {
   "something": true, 
   "status": "visiting-today"
   "vet": "DVM-Wiessman",
   "result": "J-001"
}

(I'm sure the keys here are not correct... just guessing at what they might be called)

Then the code would be a little easier to read and understand because it'd look something like


if (findByVet(currentData.vet)) {
   return currentData.result;
} else {
   return "does not exist"
}

or something along those lines.

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