Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

145
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda