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

75
Visualizações
JavaScript Array attribute change

I have an array like this.

let arr = [
  {
    "ABBRIVATION":"ISB",
    "name":"ISLAMABAD",
  },
  {
    "ABBRIVATION":"RAW",
    "name":"PINDI",
  },
  {
    "ABBRIVATION":"SWB",
    "name":"SWABI",
  },
  {
    "ABBRIVATION":"AQ",
    "name":"AQEEL",
  },
]

I want to change it to like this let me explain it a little. I want to assign the abbreviation directly to the name and the iterate through that array

let outout = [
  {
    "ISB":"ISLAMABAD"
  },
  {
    "RAW":"ISLAMABAD"
  },
  {
    "SWB":"SWABI"
  },
  {
    "AQ":"AQEEL"
  },
]

that is what I tried

let k = arr.map((item) => {
  return item.ABB = item.name
})
console.log(k) 

and here is the output

[ 'ISLAMABAD', 'PINDI', 'SWABI', 'AQEEL' ]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Here you go, use array map, simples

let arr = [
  {
    "ABBRIVATION":"ISB",
    "name":"ISLAMABAD",
  },
  {
    "ABBRIVATION":"RAW",
    "name":"PINDI",
  },
  {
    "ABBRIVATION":"SWB",
    "name":"SWABI",
  },
  {
    "ABBRIVATION":"AQ",
    "name":"AQEEL",
  },
]

let outout = arr.map(({ABBRIVATION, name}) => ({[ABBRIVATION]: name}));
console.log(outout);

about 4 years ago · Juan Pablo Isaza Relatório

0

Nothing more than a simple Array.prototype.map() needed.

let arr = [
  {
    ABBRIVATION: "ISB",
    name: "ISLAMABAD",
  },
  {
    ABBRIVATION: "RAW",
    name: "PINDI",
  },
  {
    ABBRIVATION: "SWB",
    name: "SWABI",
  },
  {
    ABBRIVATION: "AQ",
    name: "AQEEL",
  },
];

const result = arr.map(e => ({ [e.ABBRIVATION]: e.name }));
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

map over the array of objects (map returns a new array) and assign the name to a new key defined by the abbreviation.

You code works the way it does because item.ABB is undefined, but you're also assigning item.name to it which does get returned, so you just get an array of names returned.

const arr=[{ABBRIVATION:"ISB",name:"ISLAMABAD"},{ABBRIVATION:"RAW",name:"PINDI"},{ABBRIVATION:"SWB",name:"SWABI"},{ABBRIVATION:"AQ",name:"AQEEL"}];

const out = arr.map(obj => {
  return { [obj.ABBRIVATION]: obj.name };
});

console.log(out);

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