简体   繁体   中英

How use main json objects to make additional json request

I'm trying to create vehicles arrival timing based on nearby stops

stops.json

{
  "stops": [{
    "name": "Big Park",
    "code": "BP",
    "lat": 38.7273,
    "lng": 287.83838
  },
{
    "name": "Small Park",
    "code": "SP",
    "lat": 63.8373,
    "lng": 227.73689
  }]
}

If the stop “Small Park” is near me, domain.com/?name=Small%20Park request will be made to get the arrvial timings of Small Park.

{
    "results": [
        {
            "nextarr": "5",
            "subarr": "10",
            "code": "SP",
        },
        {
            "nextarr": "6",
            "subarr": "12",
            "code": "SP",
        }
    ]
}

Show Nearby (HTML)

<div class="nearby">
   <div class"card"> 
     <p>Small Park</p>
     <p>5</p>
     <p>10</p>
   </div>
</div> 
initArgs = window.location.search.slice(1)
                    .split('&')
                    .reduce(function _reduce(/*Object*/ a, /*String*/ b) {
                        b = b.split('=');
                        a[b[0]] = decodeURIComponent(b[1]);
                        return a;
                    }, {});


function get_values({ name }) {
    stops.forEach( i => {
        if (i["name"] == name) {
            // do something
        }
    })
}

get_values(initArgs);

This is about as far as I can get you with writing your app for you. I'm not sure exactly how you plan to get get the second set of data once the url argument is parsed and checked against your initial data structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM