DEV Community

Discussion on: Turn Object to Array using Object.entries()

Collapse
 
vonheikemen profile image
Heiker

That feels like a lot of work. If you are looping through a plain object the for..in loop could help.

This works.

function format_response(response) {
  let result = [];

  for(let crypto in response) {
    let data = { name: crypto };
    for(let currency in response[crypto]) {
      data.currency = currency;
      data.value = response[crypto][currency];
    }
    result.push(data);
  }

  return result;
}

const data = {"BTC":{"USD":18188.04},"ETH":{"USD":557.07}};
format_response(data);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
oryam profile image
Oryam

Thanks, interesting!
I'm sure there is more than one solution for this type of problem. I just wanted to share with something new I learned.