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

124
Views
Finding a value of one key in an array based off the value of another key

Suppose I have an object array like this:

var myArray = [
     {"sandwich": "hamburger", "cheese": "cheddar" },
     {"sandwich": "club", "cheese": "provolone" },
     {"sandwich": "reuben", "cheese": "swiss" }
]

Now, I have a variable:

 var sammy = "club"

In myArray I want to search for the item where "sandwich" is equal to the value of variable sammy and return the cheese: "provolone"

What is the simplest way to do this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using Array#find:

const
  myArray = [ {"sandwich": "hamburger", "cheese": "cheddar" }, {"sandwich": "club", "cheese": "provolone" }, {"sandwich": "reuben", "cheese": "swiss" } ],
  sammy = "club";
  
const { cheese } = myArray.find(({ sandwich }) => sandwich === sammy) || {};

console.log(cheese);

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!