JavaScript Array attribute change

Viewed 129

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' ]
5 Answers

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

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

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

Hi I have seen people answer, but most of them use the map function, I provide some other solutions, hoping to expand the thinking


Use forEach function

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

datas.forEach((obj, i, arr) => {
  const{'ABBRIVATION':k, 'name':v} = obj;
  arr[i] = {[k]:v};
});

console.log(datas);

Use flatMap function

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

const result = datas.flatMap(obj => {
  const {'ABBRIVATION':k, 'name':v} = obj;
  return {[k]:v};
});

console.log(result);

this is how you suppose to do it.

arr.reduce((d, c)=>([...d, {[c.ABBRIVATION]: c.name}]),[])

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

console.log(arr.reduce((data, current)=>([...data, {[current.ABBRIVATION]: current.name}]),[]))

Related