1

Trying to reformat the response from an API query, but running into issues. Attempted map but didn't work.

main.data.daily(symbol, 'main', 'json').then(data=> ....);

Current response format:

'data':{
  '2018-03-13':
    { '1. open': '32.8500',
      '2. high': '33.3600',
      '3. low': '32.8500',
      '4. close': '33.1400',
      '5. volume': '834894' 
    },
    ...
 }

This is the desired Format:

[{ 
 date: '2018-03-13'
 open: 32.85, 
 high: 33.36, 
 low: 33.85,
 close: 33.14, 
 volume: 855448
},
...
]

Tried the following but no cigar:

data.map(val, i, data => {
            return {
                date: i,
                open: val['1. open'],
                high: val['2. high'],
                low: val['3. low'],
                close: val['4. close'],
                volume: val['5. volume']
            }
        });

var data = {
  '2018-03-13': {
    '1. open': '32.8500',
    '2. high': '33.3600',
    '3. low': '32.8500',
    '4. close': '33.1400',
    '5. volume': '834894'
  },
}

data = data.map(val, i, data => {
  return {
    date: i,
    open: val['1. open'],
    high: val['2. high'],
    low: val['3. low'],
    close: val['4. close'],
    volume: val['5. volume']
  }
});
console.log(data)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 3
    Have you tried anything so far? StackOverflow isn't a free code-writing service, and expects you to [try to solve your own problem first](http://meta.stackoverflow.com/questions/261592). Please update your question to show what you have already tried, showing the specific problem you are facing in a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). For further information, please see [how to ask a good question](http://stackoverflow.com/help/how-to-ask), and take the [tour of the site](http://stackoverflow.com/tour) – Jaromanda X May 15 '18 at 07:39
  • 1
    Note: this question has nothing to do with promises – Jaromanda X May 15 '18 at 07:40
  • 2
    You're not trying to reformat JSON. You're trying to create an array of objects based on an object. JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder May 15 '18 at 07:40
  • @JaromandaX - Or [tag:parsing], come to that. :-) Or [tag:format]. @ JohnDonvan - Please don't tag-spam. Just use tags that really relate to the question. – T.J. Crowder May 15 '18 at 07:41
  • 1
    Added the code I was trying to use. – John Donvan May 15 '18 at 07:52
  • as `data` isn't even an array, no chance that it has `.map` :p – Jaromanda X May 15 '18 at 07:53
  • `Object.entries(response.data).map(([date,d])=>Object.assign({},{date},...Object.entries(d).map(([k,v])=>({[k.replace(/^\d+\.\s/,'')]:+v}))));` – Jaromanda X May 15 '18 at 07:54
  • Please update the snippet I made for you to a [mcve] – mplungjan May 15 '18 at 07:55

1 Answers1

5

You could reformat your objects by using Object.entries and Object.assign. For getting a new key without leading number and dot, you could take a regular expression which separates the wanted part for a new key.

var data = { '2018-03-13': { '1. open': '32.8500', '2. high': '33.3600', '3. low': '32.8500', '4. close': '33.1400', '5. volume': '834894' }, '2018-03-12': { '1. open': '32.3900', '2. high': '32.8050', '3. low': '32.2800', '4. close': '32.6800', '5. volume': '855448' } },
    result = Object
        .entries(data)
        .map(([date, object]) => Object.assign(
            { date },
            ...Object.entries(object).map(([k, v]) => ({ [k.match(/\w+$/)]: +v })))
        );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392