1

const data = [
  {
    'UTMEH5': {
      descr: {
        ordertype: 'limit',
        pair: 'XBT/USD',
        price: '5000.00000',
      },
      status: 'open',
      vol: '0.00200000',
    },
  },
  {
    '3F2TYE': {
      descr: {
        ordertype: 'limit',
        pair: 'XBT/USD',
        price: '10000.00000'
      },
      status: 'open',
      vol: '0.00200000',
    },
  },
]

const orders = data.map((order) => {
          return Object.entries(order).map(([key, value]) => ({
            orderid: key,
            pair: value['descr']['pair'],
            vol: value['vol'],
            price: value['descr']['price'],
            ordertype: value['descr']['ordertype'],
            status: value['status'],
          }))})
          
 console.log(orders)

With the above code I am getting this:

[
  [
    {
      "orderid": "UTMEH5",
      "pair": "XBT/USD",
      "vol": "0.00200000",
      "price": "5000.00000",
      "ordertype": "limit",
      "status": "open"
    }
  ],
  [
    {
      "orderid": "3F2TYE",
      "pair": "XBT/USD",
      "vol": "0.00200000",
      "price": "10000.00000",
      "ordertype": "limit",
      "status": "open"
    }
  ]
]

but I want this:

[
    {
      "orderid": "UTMEH5",
      "pair": "XBT/USD",
      "vol": "0.00200000",
      "price": "5000.00000",
      "ordertype": "limit",
      "status": "open"
    },
    {
      "orderid": "3F2TYE",
      "pair": "XBT/USD",
      "vol": "0.00200000",
      "price": "10000.00000",
      "ordertype": "limit",
      "status": "open"
    }
]

With the duplicate suggestion it looks like I can use .flat(), but that seems like a roundabout way to get what I'm looking for. Is there a better more straightforward way?

user4584963
  • 2,403
  • 7
  • 30
  • 62
  • I was thinking my approach is wrong, instead of doing it the wrong way and then flattening it, I was wondering if there is a better way to begin with – user4584963 Jan 17 '21 at 14:23
  • Ohh, I've understood now and 've voted to reopen. However, with the way the question is put together, it's easier to get misled though. How about you put your code first and explain the situation a bit better? – Tibebes. M Jan 17 '21 at 14:28
  • @Tibebes.M ok done – user4584963 Jan 17 '21 at 14:33

1 Answers1

1

You can use flatMap() instead of map() for such case.

const data = [{
    'UTMEH5': {
      descr: {
        ordertype: 'limit',
        pair: 'XBT/USD',
        price: '5000.00000',
      },
      status: 'open',
      vol: '0.00200000',
    },
  },
  {
    '3F2TYE': {
      descr: {
        ordertype: 'limit',
        pair: 'XBT/USD',
        price: '10000.00000'
      },
      status: 'open',
      vol: '0.00200000',
    },
  },
]

const orders = data.flatMap((order) => Object.entries(order).map(([key, value]) => ({
  orderid: key,
  vol: value['vol'],
  status: value['status'],
  ...value['descr'], // you can use spread operator here for brevity as well
})))

console.log(orders)
Tibebes. M
  • 6,940
  • 5
  • 15
  • 36