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

428
Views
Better alternative to finding a value in array of arrays instead of using indices? (JavaScript)

I have JavaScript data that looks like this:

[
  ["firstName", "bob"],
  ["lastName", "smith"],
  ["address", "123 Main St]
]

The second values in each array (bob, smith, 123 main st) are entered from the user. I'm simply wanting to print the entered value. I know I can use indices (such as [0][1] to capture "bob") but is there a better way to do that? I know JavaScript objects have a .get() method where you can enter the key and have the value returned (such as .get("firstName") to return "bob"). Is there someway to do that with arrays?

Thanks!

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

this way...
you may learn about Object.fromEntries()

const dataArr2 = 
  [ [ "firstName", "bob"]
  , [ "lastName", "smith"]
  , [ "address", "123 Main St"]
  ]
  
let obj = Object.fromEntries( dataArr2 )

console.log( obj.firstName ) // bob
console.log( obj.lastName )  // smith
console.log( obj.address )   // 123 Main St

about 4 years ago · Santiago Gelvez Report

0

There are multiple ways to do so. Please check the below given examples.

const personArr = [
  ["firstName", "bob"],
  ["lastName", "smith"],
  ["address", "123 Main St"],
];

console.log(new Map(personArr));
console.log(Object.fromEntries(personArr));

function toObject(arr = []) {
  return arr.reduce((acc, [key, value]) => {
    acc[key] = value;
    return acc;
  }, {});
}
console.log(toObject(personArr));

const { firstName, lastName, address } = toObject(personArr);
console.log(firstName, lastName, address);

about 4 years ago · Santiago Gelvez 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!