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

140
Views
JavaScript: Array + Object to Sorted Array

Have been stuck on this issue for 6+ hours. I have started to learn JavaScript few months ago, so far so good, but cant resolve this one.

const arrHeader["name","street","city","state","zip","phone","fax","custom"];

object as follows

const obj = {
      zip: "01001"
      phone: "77894644"
      city: "Albany",
      state: NY,
      name: "John",
      street: "123 Main St"
      custom: "best user"
      fax: ""
    };

It is possible to get output as array (not associated array/object) sorted based on arrHeader? the lengh of array/object is the same. Please note blank for fax, the index in result should correspond to arrHeader, even if the value is null. Than you. Any suggestion are appreciated.

Output should look like for the given object. Not sure if " " best to keep index same.

var result["John","123 Main St","Albany","NY","01001","77894644"," ","best user"];

Thank you.

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

0

If I understand correctly, it seems like you want to perform a mapping operation on your array with .map(). This will allow you to take each element from arrHeader and map it to its value from your obj by using the element as a key obj[key]:

const arrHeader = ["name","street","city","state","zip","phone","fax","custom"];

const obj = { zip: "01001", phone: "77894644", city: "Albany", state: "NY", name: "John", street: "123 Main St", custom: "best user", fax: "" };
const res = arrHeader.map(key => obj[key]);
console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

Please find Array.reduce implementation of your requirement.

const arrHeader = ["name","street","city","state","zip","phone","fax","custom"];
const obj = {
  zip: "01001",
  phone: "77894644",
  city: "Albany",
  state: "NY",
  name: "John",
  street: "123 Main St",
  custom: "best user",
  fax: ""
};
const result = arrHeader.reduce((acc, curr) => {
  acc.push(obj[curr]);
  return acc;
}, []);
console.log(result);

Array.map implementation

const arrHeader = ["name","street","city","state","zip","phone","fax","custom"];
const obj = {
  zip: "01001",
  phone: "77894644",
  city: "Albany",
  state: "NY",
  name: "John",
  street: "123 Main St",
  custom: "best user",
  fax: ""
};
const result = arrHeader.map((node) => obj[node])
console.log(result);

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!