• Jobs
  • About Us
  • Jobs
    • Home
    • Jobs
    • Courses and challenges
  • Businesses
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

80
Views
Transformar objeto complejo en valor clave

tengo una matriz

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

Y quiero transformar la matriz a esto

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

¿Hay una mejor manera que esta?

 const result = {} arr.reduce((accum, val) => { Object.assign(result, { [val.symbol]: val.name }); }, {}) console.log(result)
over 3 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Use Object.entries() y cada objeto que los devolverá como una matriz de matrices; cada sub-matriz será un par clave/valor ( [key, value] ) y luego use Object.assign() para crear un nuevo objeto ( {[key]: value} ) para regresar. Luego aplánelo para que estén todos en una matriz.

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

over 3 years ago · Juan Pablo Isaza Report

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)

Aquí está la respuesta a por qué la clave del objeto se establece en ese formato: Javascript establece la clave del objeto por variable

over 3 years ago · Juan Pablo Isaza Report

0

La respuesta es

 Object.fromEntries(arr.map(({symbol, name}) => [symbol, name]))
over 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.

Andres GPT

Show me some job opportunities
There's an error!