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

83
Visualizações
how to search and retrun value from array

Trying, find and return a value from array using JavaScript- with dynamic inputs

const drawers = [
  {
    "name": "locations",
    "values": [
      {
        "value": "dana-point-ca",
        "label": "Dana Point, CA"
      },
      {
        "value": "bronx-new-york",
        "label": "Bronx, New York"
      },
      {
        "value": "new-york-ny",
        "label": "New York, NY"
      }
    ]
  },
  {
    "name": "programAreas",
    "values": [
      {
        "value": "coral-conservation",
        "label": "CORAL CONSERVATION"
      }
    ]
  }
]

Input keys are dynamic, if it is locations and value is bronx-new-york then it should return Bronx, New York;

let lbl = drawers.find(o => o.name === 'string 1').label;
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Use array.find in each values until you find the answer.

const drawers = [
  {
    "name": "locations",
    "values": [
      {
        "value": "dana-point-ca",
        "label": "Dana Point, CA"
      },
      {
        "value": "bronx-new-york",
        "label": "Bronx, New York"
      },
      {
        "value": "new-york-ny",
        "label": "New York, NY"
      }
    ]
  },
  {
    "name": "programAreas",
    "values": [
      {
        "value": "coral-conservation",
        "label": "CORAL CONSERVATION"
      }
    ]
  }
]

function getLabel(x) {
  for (const nameValues of drawers) {
    const values = nameValues.values
    const item = values.find(v => v.value === x)
    if (item !== undefined) {
      return item.label
    }
  }  
}

getLabel("bronx-new-york")  // 'Bronx, New York'
getLabel("coral-conservation")  // 'CORAL CONSERVATION'
getLabel("Value that does not exist") // undefined
about 4 years ago · Juan Pablo Isaza Relatório

0

for(let i = 0 ; i < drawers.length ; i++){
    let lbl = drawers[i].values.find(o => o.label === "Bronx, New York").label;
    console.log(lbl)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

There is a couple of ways to achieve that. As below, you will get all the possible results. However, you might need to deconstruct the arrays in order to get the strings.

const drawers = [
  {
    "name": "locations",
    "values": [
      {
        "value": "dana-point-ca",
        "label": "Dana Point, CA"
      },
      {
        "value": "bronx-new-york",
        "label": "Bronx, New York"
      },
      {
        "value": "new-york-ny",
        "label": "New York, NY"
      }
    ]
  },
  {
    "name": "programAreas",
    "values": [
      {
        "value": "coral-conservation",
        "label": "CORAL CONSERVATION"
      }
    ]
  }
]

const enteredValue = "bronx-new-york";
const resultArrays = []

const onSearchLocation = () => {
 drawers.find(location => {
    const labels = location.values.map((place => { 
      const array = [];
      if(place.value === enteredValue) {
          array.push(place.label);
      } 
      
      if(array.length > 0) {
          resultArrays.push(array);
      }
     }
   ))
  })}

onSearchLocation();

console.log(resultArrays);
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