简体   繁体   English

如何将 rrule 日期值从数组转换为自定义字符串

[英]How to transform rrule date value from array to custom string

I have an array like this:我有一个这样的数组:

     [{
evening_feeding: false
    evening_feeding_time: "19:00"
    feeding_frequency_rule: **"FREQ=DAILY;INTERVAL=2"**
    id: 890
    morning_feeding: true
    morning_feeding_time: "04:00"
    noon_feeding: false
    noon_feeding_time: "12:00"
    pen_id: 299
    start_feeding_date: "2020-07-07"
}]

I would like just to display this feeding_frequency_rule: "FREQ=DAILY;INTERVAL=2" as for example any string like Every day in my html view.我只想在我的 html 视图中显示这个 feed_frequency_rule: "FREQ=DAILY;INTERVAL=2"作为例如每天的任何字符串。 Any suggestion for this?对此有何建议?

You could create a function to format the rule, this would first parse the rule, then return a human readable string for the given rule.您可以创建一个 function 来格式化规则,这将首先解析规则,然后为给定规则返回一个人类可读的字符串。

If we can't find a match, we'll just return the raw FREQ value.如果找不到匹配项,我们将只返回原始 FREQ 值。

I've created a couple of rows of sample data, then displayed in a Bootstrap 4 table.我创建了几行示例数据,然后显示在 Bootstrap 4 表中。

 let feedData = [ { evening_feeding: false, evening_feeding_time: "19:00", feeding_frequency_rule: "FREQ=DAILY;INTERVAL=2", id: 890, morning_feeding: true, morning_feeding_time: "04:00", noon_feeding: false, noon_feeding_time: "12:00", pen_id: 299, start_feeding_date: "2020-07-07" }, { evening_feeding: false, evening_feeding_time: "19:00", feeding_frequency_rule: "FREQ=WEEKLY;INTERVAL=2", id: 891, morning_feeding: true, morning_feeding_time: "04:00", noon_feeding: false, noon_feeding_time: "12:00", pen_id: 300, start_feeding_date: "2020-07-07" } ] function formatFeedingFrequencyRule(rule) { let freq = rule.split(";")[0].split("=")[1].trim(); switch (freq) { case "DAILY": return "Every day"; case "WEEKLY": return "Every week"; default: return freq; } } feedData.forEach(obj => { document.getElementById("tbody_1").innerHTML += formatRow(obj); }); function formatRow(obj) { return `<tr><td>${obj.id}</td><td>${formatFeedingFrequencyRule(obj.feeding_frequency_rule)}</td>` }
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> <div class="container"> <h5>Feeding Rules</h5> <table class="table"> <thead> <tr> <th>ID</th> <th>Feeding Frequency</th> </tr> </thead> <tbody id="tbody_1"> </tbody> </table> </div> </body>

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

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