【问题标题】:How can I read a property-name of an object with blanks如何读取带有空格的对象的属性名称
【发布时间】:2021-06-06 22:49:01
【问题描述】:
const scraper = require("table-scraper"); //scrape a html table
scraper
   .get("https://www.gkd.bayern.de/de/meteo/wind/isar/ammerseeboje-16601050/messwerte/tabelle") 
      //that is the list of wind values for each hour
   .then(function (tableData) {
      console.log(tableData[1]); //that is the table as an object
  })

控制台返回:

 [
    { Datum: '05.06.2021 21:00', 'Wind [m/s]': '5,7' },
    { Datum: '05.06.2021 20:00', 'Wind [m/s]': '5,4' },
....
]

tableData[1][0].Datum - ok 定义数据
但是
tableData[1][0].'Wind [m/s]' - 将返回未定义...
我怎样才能获得例如的价值。 5,7?
谢谢

【问题讨论】:

标签: javascript


【解决方案1】:

const tableData = [{
    Datum: '05.06.2021 21:00',
    'Wind [m/s]': '5,7'
  },
  {
    Datum: '05.06.2021 20:00',
    'Wind [m/s]': '5,4'
  }
]


console.log(tableData[0]['Wind [m/s]'])

【讨论】:

    【解决方案2】:

    我不确定我是否理解这些问题,但如果您尝试返回值 5,7 我想您可能需要这样的东西:

    const data = [{
        Datum: '05.06.2021 21:00',
        'Wind [m/s]': '5,7'
      },
      {
        Datum: '05.06.2021 20:00',
        'Wind [m/s]': '5,4'
      }
    ]
    
    console.log(data[0][`Wind [m/s]`])
    

    【讨论】:

      【解决方案3】:

      const obj =  [
          { Datum: '05.06.2021 21:00', 'Wind [m/s]': '5,7' },
          { Datum: '05.06.2021 20:00', 'Wind [m/s]': '5,4' }]
          
      console.log(obj[0]['Wind [m/s]'])
      
      console.log(Object.values(obj[0])[1]);

      【讨论】:

        猜你喜欢
        • 2015-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-23
        • 1970-01-01
        相关资源
        最近更新 更多