循环通过JSON&比较时间

时间:2016-05-19 02:31:26

标签: javascript jquery json

我想要完成的是循环遍历此JSON,并比较“start_time”和“end_time”以确保时间不重叠。我在实现这个方面遇到了麻烦。

我发现了这个:validate two times但它没有任何意义,也没有使用JSON,但它是我发现的最接近的。我可以使用jQuery来做到这一点吗?

{
    "Line_1":{
        "artist":"Audien",
        "day":"1",
        "start_time":"13:00",
        "end_time":"14:00",
        "stage":"main"
    },
    "Line_2":{
        "artist":"Slushii",
        "day":"1",
        "start_time":"13:30",
        "end_time":"14:30",
        "stage":"eclipse"
    },
    "Line_3":{
        "artist":"DJ Snake",
        "day":"1",
        "start_time":"15:00",
        "end_time":"16:00",
        "stage":"main"
    },
    "Line_4":{
        "artist":"Marshmello",
        "day":"2",
        "start_time":"14:15",
        "end_time":"15:15",
        "stage":"horizon"
    }
}

预期产出:

  

Audien& Slushii冲突!

     

DJ Snake与任何人都没有冲突!

     

Marshmello不与任何人发生冲突!

*注意第1天和第1天2

2 个答案:

答案 0 :(得分:0)

我的解决方案:

var json = {
  "Line_1":{
    "artist":"Audien",
    "day":"1",
    "start_time":"13:00",
    "end_time":"14:00",
    "stage":"main"

  },
  "Line_2":{
    "artist":"Slushii",
    "day":"1","start_time":"13:30",
    "end_time":"14:30",
    "stage":"eclipse"

  },
  "Line_3":{
    "artist":"DJ Snake",
    "day":"1",
    "start_time":"15:00",
    "end_time":"16:00",
    "stage":"main"

  },
  "Line_4":{
    "artist":"Marshmello",
    "day":"2",
    "start_time":"17:15",
    "end_time":"15:15",
    "stage":"horizon"

  }
};

function timeToDate(timeStr) {
    var whateverDate = '01/01/1980  '; 
    return Date.parse(whateverDate + timeStr);
}

for (item in json) {
    var st = json[item].start_time;
    var et = json[item].end_time;
    var datesCorrect = (timeToDate(st) < timeToDate(et)) ? 'true' : 'false';
    console.log(item + ' dates correct: ' + datesCorrect);
};

演示:https://jsfiddle.net/dhf89twr/1/

答案 1 :(得分:0)

这是一个相当冗长的原型,供您学习。它使用moment.jstwix.js

演示: https://jsfiddle.net/JAAulde/5v7yksk3/4/

原型代码的HTML:

<ul id="output"></ul>

JS for prototye code

var data = {
        "Line_1":{
            "artist":"Audien",
            "day":"1",
            "start_time":"13:00",
            "end_time":"14:00",
            "stage":"main"
        },
        "Line_2":{
            "artist":"Slushii",
            "day":"1",
            "start_time":"13:30",
            "end_time":"14:30",
            "stage":"eclipse"
        },
        "Line_3":{
            "artist":"DJ Snake",
            "day":"1",
            "start_time":"15:00",
            "end_time":"16:00",
            "stage":"main"
        },
        "Line_4":{
            "artist":"Marshmello",
            "day":"2",
            "start_time":"14:15",
            "end_time":"15:15",
            "stage":"horizon"
        }
    },
    tmp_day = '2000-01-01',
    outer_key,
    outer,
    inner_key,
    inner,
    tmp_range,
    checked = {},
    conflict_found = {},
    conflicts = [],
    i;

for (outer_key in data) {
    if (Object.prototype.hasOwnProperty.call(data, outer_key)) {
        outer = data[outer_key];

        tmp_range = moment(tmp_day + 'T' + outer.start_time).twix(tmp_day + 'T' + outer.end_time);

        checked[outer_key] = true;

        for (inner_key in data) {
            if (Object.prototype.hasOwnProperty.call(data, inner_key) &&
                outer_key !== inner_key &&
                !checked[inner_key]
            ) {
                inner = data[inner_key];

                if (outer.day === inner.day &&
                    (
                        tmp_range.contains(tmp_day + 'T' + inner.start_time) ||
                        tmp_range.contains(tmp_day + 'T' + inner.end_time)
                    )
                ) {
                    conflict_found[outer_key] = true;
                    conflict_found[inner_key] = true;

                    conflicts.push([
                        outer_key,
                        inner_key
                    ]);
                }
            }
        }
    }
}

// Output:
document.getElementById('output').innerHTML = '';

for (i = 0; i < conflicts.length; i++) {
    document.getElementById('output').innerHTML += '<li><strong>' + data[conflicts[i][0]].artist + '</strong> conflicts with <strong>' + data[conflicts[i][1]].artist + '</strong></li>';
}

for (outer_key in data) {
    if (Object.prototype.hasOwnProperty.call(data, outer_key) &&
        !conflict_found[outer_key]
    ) {
        document.getElementById('output').innerHTML += '<li><strong>' + data[outer_key].artist + '</strong> does not conflict with anyone</li>';
    }
}
相关问题