简体   繁体   English

通过函数获取有关太阳位置的数据。 怎么样?

[英]Get data about the position of our sun through a function. How?

I am using mourner/suncalc to get information about our sun. 我正在使用mourner / suncalc获取有关太阳的信息。 I want to get all the times where the position of our sun will be, using SunCalc.getTimes() , but to prevent using "miles long" variables, I want to call each data through a function. 我想使用SunCalc.getTimes()获得所有太阳位置的时间,但是为了防止使用“英里长”变量,我想通过一个函数调用每个数据。

I am thinking about something like this: 我在想这样的事情:

function sun_times(string) {
    var sun_times = SunCalc.getTimes(new Date(), 54.528486, -7.569268);
    return (sun_times.string.getHours().toString() < 10 ? '0' + sun_times.string.getHours().toString() : sun_times.string.getHours().toString());
}

console.log(sun_times('dawn'));

This example returns (as expected) TypeError: sun_times.string is undefined . 本示例返回(按预期方式) TypeError: sun_times.string is undefined

Instead of entering sun_times.dawn.getHours().toString() + ':' + sun_times.dawn.getMinutes().toString() for every time at dawn, dusk, golden hour, nadir, nautical dawn, and so on, I just want to enter the function and then the data I want to print out, for an example sun_times('dawn') . 而不是每次在黎明,黄昏,黄金时段,最低点,航海黎明等时间输入sun_times.dawn.getHours().toString() + ':' + sun_times.dawn.getMinutes().toString()来代替,我只想输入函数,然后输入要打印的数据,例如sun_times('dawn')

How can I accomplish this the best way? 我怎样才能最好地做到这一点?

A JavaScript object works like a dictionary structure. JavaScript对象的工作方式类似于字典结构。 We use the [] indexing operator to access properties only known at runtime. 我们使用[]索引运算符访问仅在运行时已知的属性。 It's like array access, but with strings instead of numbers. 就像数组访问一样,但是使用字符串而不是数字。

function sun_times(string) {
    var sun_times = SunCalc.getTimes(new Date(), 54.528486, -7.569268);
    return (sun_times[string].getHours().toString() < 10 ? '0' + sun_times[string].getHours().toString() : sun_times[string].getHours().toString());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过jquery get函数获取html文件。 Django的 - Getting an html file through a jquery get function. Django .filter 不是一个函数。 尝试从 API 数据中搜索名称 - .filter is not a function. Trying to search through names from API data Ajax无法实现成功功能。 始终错误功能。 无法将数据发布到另一个文件 - Ajax doesn't get to success function. Always error function. It fails to post data to another file 未捕获的TypeError:data.push不是函数。 我怎样才能解决这个推送错误? - Uncaught TypeError: data.push is not a function. How can I get around this push error? 通过函数传递参数时出错。 - Error in passing argument through a function. 如何通过javascript函数捕获文本中的更改。 JavaScript更改事件在这种情况下不起作用 - How to capture change in text when that change is through javascript function. Javascript change event is not working in this case 使用包含函数的 JSON-Object 进行函数调用。 如何获取函数外的返回变量? - function call with JSON-Object, which contains a function. how to get return variable outside the function? 无法深入了解订阅回调 function 中的响应数据 object。 为什么? - Can't get deeper into the response data object in subscribe's callback function. Why? 如何从 http 回调 function 调用 function(在单独的文件中)。app.get("/", callbackFun) - How to call a function (in separate file) from http callback function. app.get("/", callbackFun) 随机ID function。ID的长度和Id的数量通过prompt() - Random ID function. Length of ID and amount of Id through prompt()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM