Regular expressions in AJax requests

0

I have a REST request that returns a json with values

{"Meta Data":{"1. Information":"Intraday (1min) prices and volumes","2. Symbol":"MSFT","3. Last Refreshed":"2018-05-30 16:00:00","4. Interval":"1min","5. Output Size":"Compact","6. Time Zone":"US/Eastern"},"Time Series (1min)":{"2018-05-30 16:00:00":{"1. open":"99.0000","2. high":"99.0500","3. low":"98.9100","4. close":"98.9500","5. volume":"2233252"}, ......

I ask it this way:

    $(document).ready(function () {
$.ajax({
    url: "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=*******"
}).then(function (data) {
    console.log(JSON.stringify(data));
});
});

The problem is that every time I return the field is with different values since the date changes.

Asking in the forum they told me how to represent the data of the moment. But the data changes so I've thought about adding a regular expression:

data["Time Series (1min)"]["^([1-9]|([012][0-9])|(3[01]))-([0]{0,1}[1-9]|1[012])-\d\d\d\d [012]{0,1}[0-9]:[0-6][0-9]$"]["1. open"]

But it does not work, can I add elements of regular expressions the way I'm doing, any help?

    
asked by Eduardo 31.05.2018 в 21:07
source

1 answer

0

If you have to look at the keys of your data["Time Series (1min)"] because it contains more things, I advise you something like this:

var clavesBuenas = Object.keys(data["Time Series (1min)"]).filter(function(clave) {
  return (clave.test(/tuJsRegExHere/))
})

With this you have an array of all the keys that match your regular expression

If you want later:

dataBienOrganizada = clavesBuenas.map(function (clave) {
  return { fecha: clave, dato: this[clave] }
}, data["Time Series (1min)"])

See the documentation for Regex.test, Object.keys, Array.map, Array.filter, if you need it

    
answered by 02.06.2018 / 15:10
source