1

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' ]
0

5 Answers 5

4

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

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

3

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

Comments

0

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

Comments

-2

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}]),[]))

3 Comments

why are you suppose to do it like this? why must you abuse reduce to work just like map? why? please explain why this is at all better than using map?
How is reduce the better option here? You basically mapped the array without using map??
I agree that this is useless use of reduce. This code is about 26% slower than the .map solution according to JSBench.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.