简体   繁体   中英

Parsing from JSON array, two objects

So I thought about using Jquery, and using the getJSON but I couldn't quite figure out how to get it to work, basically what I am trying to achieve is get the ['statename'], and the ['city'].

I was wondering what would be the.. simplest way to get them, and then have each of those results saved into ['statename'], and ['city']

So that way I can call them back in the url as ${statename} and ${city}..

Here's the code I have so far :

scripts.js

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        const {latitude, longitude} = position.coords;
        let pos = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };
      const currentLocation = `https://geocode.xyz/${latitude},${longitude}?json=1`
      console.log(currentLocation)
    })};

scrapper.js

debug = require ('../models/conn');
const puppeteer = require('puppeteer');
const axios = require('axios');
const cheerio = require('cheerio');

async function searchJobs(i) {
   const url = await axios.get('https://indeed.com/jobs?q=Web+Developer&l=Atlanta&fromage=last')
    // return fetch(`${url}${i}`)
        .then(response =>  response)

        .then(res => {
            const jobs = [];
            const $ = cheerio.load(res.data);

            $('.result').each((index, element) => {
                const title = $(element).children('.title').text();
                const linkToJob = $(element).children('.title').children('a').attr('href')
                const body = $(element).children('.summary').text();
                jobs[index] = { title, linkToJob, body };
            });
            console.log(jobs);
            return jobs;
            // Prints tbe second child of results class results for that page in console.
            //    console.log($('.result').children().eq(1).text());
        });
        return url;
};


// async function userCity(lat, long){
//         const currentLocation = `https://geocode.xyz/${lat},${long}?json=1`
//         await axios.get(currentLocation).then(response => {
//             console.log(response['city'], response['statename']);
//         })
//     }
module.exports = searchJobs;

If you need or are wondering about the view files ask, I think these are the only two files really relevant to the question.. thank you :)

If I'm going to receive down votes, should at-least let me know why

Fixed it with the following code..

    $.getJSON(`https://geocode.xyz/${latitude},${longitude}?json=1`, function (data) {
        let state = `${data.statename}`
        let city = `${data.city}`
        console.log(state)
        console.log(city)
    });

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