-3

I have an array

const arr = [
  {
    "id": 1,
    "name": "Bitcoin",
    "symbol": "BTC",
    "slug": "bitcoin",
    "rank": 1,
    "is_active": 1,
    "first_historical_data": "2013-04-28T18:47:21.000Z",
    "last_historical_data": "2022-02-18T11:39:00.000Z",
    "platform": null
  },
  {
    "id": 2,
    "name": "Litecoin",
    "symbol": "LTC",
    "slug": "litecoin",
    "rank": 20,
    "is_active": 1,
    "first_historical_data": "2013-04-28T18:47:22.000Z",
    "last_historical_data": "2022-02-18T11:39:00.000Z",
    "platform": null
  }
]

And I want to transform the array to this

{
  "BTC": "Bitcoin",
  "LTC": "Litecoin",
}

Is there a better way than this?

const result = {}
arr.reduce((accum, val) => {
  Object.assign(result, { [val.symbol]: val.name });
}, {})

console.log(result)
Rodrigo
  • 135
  • 4
  • 45
  • 107

3 Answers3

0

Use Object.entries() an each object which will return them as an array of arrays -- each sub-array will be a key/value pair ([key, value]) then use Object.assign() to create a new object ({[key]: value}) to return. Then flatten it so they are all in one array.

const arr=[{id:1,name:"Bitcoin",symbol:"BTC",slug:"bitcoin",rank:1,is_active:1,first_historical_data:"2013-04-28T18:47:21.000Z",last_historical_data:"2022-02-18T11:39:00.000Z",platform:null},{id:2,name:"Litecoin",symbol:"LTC",slug:"litecoin",rank:20,is_active:1,first_historical_data:"2013-04-28T18:47:22.000Z",last_historical_data:"2022-02-18T11:39:00.000Z",platform:null}];

const conv = array => {
 let objects = array.map(obj => Object.entries(obj).map(([key, val]) => Object.assign({}, {[key]: val})));
 return objects.flat();
};

console.log(conv(arr));
zer00ne
  • 41,936
  • 6
  • 41
  • 68
0

let arr = [
  {
    "id": 1,
    "name": "Bitcoin",
    "symbol": "BTC",
    "slug": "bitcoin",
    "rank": 1,
    "is_active": 1,
    "first_historical_data": "2013-04-28T18:47:21.000Z",
    "last_historical_data": "2022-02-18T11:39:00.000Z",
    "platform": null
  },
  {
    "id": 2,
    "name": "Litecoin",
    "symbol": "LTC",
    "slug": "litecoin",
    "rank": 20,
    "is_active": 1,
    "first_historical_data": "2013-04-28T18:47:22.000Z",
    "last_historical_data": "2022-02-18T11:39:00.000Z",
    "platform": null
  }
]

// As suggested, here it is without having to create an initial array

let alternativeArray = arr.map((val) => {
  return {[val.symbol]: val.slug}
})

console.log(alternativeArray)

Here is the answer to why the object key is being set in that format: Javascript set object key by variable

Cypherjac
  • 859
  • 8
  • 17
  • Why would you push an object into a different array if ```map``` already returns a new array? – Mod3rnx Feb 18 '22 at 12:42
  • Because we are creating an object inside of the map function, and that object is what is pushed into the array. I would like to see your version without an initially created array, and push the objects into it – Cypherjac Feb 18 '22 at 12:48
  • My comment was related to your unedited answer, now everything makes sense. – Mod3rnx Feb 18 '22 at 13:05
-1

The answer is

Object.fromEntries(arr.map(({symbol, name}) => [symbol, name]))
Rodrigo
  • 135
  • 4
  • 45
  • 107
  • 2
    While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. – Luca Kiebel Feb 18 '22 at 12:00
  • Since this doesn't answer your question anymore, you can delete this answer. – Felix Kling Feb 18 '22 at 12:23