簡體   English   中英

JSON 數組項返回未定義

[英]JSON array items return undefined

我從 JSON object 中獲取一個數組,並遍歷子數組的每個項目以創建一個新數組,其中包含每個孩子的 id 和名稱。 但是,當嘗試使用 .id 或 .name 時,會返回 undefined 導致一個空白數組。

const functions = require('firebase-functions');

const cors = require('cors')({ origin: true });

const cheerio = require('cheerio');
const getUrls = require('get-urls');
const fetch = require('node-fetch');

const scrapeteamtags = (text) => {
    const urls = Array.from( getUrls(text) );
    const requests = urls.map(async url => {

        const res = await fetch(url);
        const html = await res.text();
        const $ = cheerio.load(html);
        
        const getTeamlist = JSON.parse($('body').text())  
        
        var teams = {
            newgames: []
        }

        getTeamlist.SSResponse.children.map(function(item) {
            teams.newgames.push({   
                "event-id": item.event.id
            });            
            console.log(teams.newgames)
        })
        
        console.log(teams.newgames[0])

        return {
            teams
            //"game": getTeamlist.SSResponse.children[0].event.name
        }
    });
    
    return Promise.all(requests);
}

exports.scraper = functions.https.onRequest( async (request, response) => {
    cors(request, response, async () => {

        const body = (request.body);
        const data = await scrapeteamtags(body.text);

        response.send(data)

    });
});

JSON object(實際 object 包含子數組中的許多項目):

{
"SSResponse":{
   "xmlns":"http://schema.openbet.com/SiteServer/2.31/SSResponse.xsd",
   "children":[
      {
         "event":{
            "id":"230526710",
            "name":"Sevilla v Real Valladolid",
            "children":[
               {
                  "market":{
                     "id":"452880267",
                     "eventId":"230526710",
                     "children":[
                        {
                           "outcome":{
                              "id":"1080419229",
                              "marketId":"452880267",
                              "name":"Draw",
                              "children":[
                                 {
                                    "price":{
                                       "id":"1",
                                       "priceDec":"4.25"
                                }}]}},
                        {
                           "outcome":{
                              "id":"1080419232",
                              "marketId":"452880267",
                              "name":"Sevilla",
                              "children":[
                                 {
                                    "price":{
                                       "id":"2",                                    
                                       "priceDec":"1.40"
                                }}]}},
                        {
                        "outcome":{
                            "id":"1080419233",
                            "marketId":"452880267",
                            "name":"Real Valladolid",                                  
                            "children":[
                                {
                                "price":{
                                    "id":"3",
                                    "priceDec":"9.50"
                                }}]}}]}}]}},
      {
         "responseFooter":{
            "cost":"238",
            "creationTime":"2020-06-26T17:04:39.617Z"
        }}]}}

我感興趣的數組部分是第一個孩子的 id 和名稱,然后是嵌套子對象的 id 和“priceDec”,最初我只是想將第一個 id 添加到數組中然后工作關於獲取下一個子對象。 謝謝

有這個代碼:

getTeamlist.SSResponse.children.map(function(item) {
      teams.newgames.push({   
          "event-id": item.event.id
      });            
      console.log(teams.newgames)
  })

SSResponse鍵中的所有孩子都沒有事件鍵,這就是為什么你得到undefined ,在map function 中試試這個:

if(Object.keys(item).includes('event')){
      teams.newgames.push({   
          "event-id": item.event.id,
          "event-name": item.event.name
      });
    }

基本上要求一個名為“事件”的鍵,然后獲取數據。 您可以查看stackblitz 的此鏈接作為示例。 希望這對你有幫助!

嗨,Ethan,就訪問 object 和陣列中的字段而言,您可以嘗試。 你可以簡單地使用。 運算符如下: outcome.id or outcome.name名。對於子數組,您可以使用: outcome.children[0].price.id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM