Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda