Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

80
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!