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

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

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 Denunciar

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 Denunciar

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 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