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

72
Views
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 answers
Answer question

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 Report

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 Report

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