如果不在数组中 -> array.push

if not in array -> array.push

我正在尝试在 javascript 中使用来自 JSON 提要的独特元素创建一个数组。 也许我在 php 中想了很多,但有一个简单的脚本,如下所示。

$this = array();
foreach($array as $key => $value) {
    if (!in_array($value,$this)) {
        array_push($this,$value);
    }
}

在 JS 中它不会:

var this = new Array();
$.each(data, function(i,field) {
    var value = field['needed'];
    if (!$.inArray(value, this)) {
        this.push(value);
    }
}

它只是将每个 field['needed'] 添加到 this

完整代码让你知道我是如何得到我的 JSON

$(function() {
    $("#wtf").empty();
    $.getJSON("http://localhost/ahsmedia/openingsuren/api.php?first=" + cal.currentDate.format('d-m-Y'),
    function(data) {
        var camp = new Array();
        $.each(data, function(i,field) {
            var dt = field['datum'];
            var ca = field['campus'];
            var ca = ca.toLowerCase();
            if ($.inArray(ca, camp)) {
                camp.push(ca);
            }

            $("#wtf").append(field['campus'] + '  ' + cal.currentDate.format('d-m-Y') + ': ' +  " " + field['open'] + "-" + field['close'] + " " + field['type'] +  "<br />");
        });   
        console.log(camp);   
    });     
});

JSON 看起来像:

[
    {
        "datum": "2015-01-07",
        "type": "normal",
        "campus": "Kantienberg",
        "open": "08:00:00",
        "close": "18:00:00"
    },
    {
        "datum": "2015-01-07",
        "type": "normal",
        "campus": "Kattenberg",
        "open": "08:00:00",
        "close": "18:00:00"
    },
    {
        "datum": "2015-01-07",
        "type": "normal",
        "campus": "Mariakerke",
        "open": "08:30:00",
        "close": "18:00:00"
    },
    {
        "datum": "2015-01-07",
        "type": "normal",
        "campus": "Sint-Amandsberg",
        "open": "09:30:00",
        "close": "11:30:00"
    },
    {
        "datum": "2015-01-07",
        "type": "normal",
        "campus": "Sint-Amandsberg",
        "open": "12:00:00",
        "close": "17:30:00"
    },
    {
        "datum": "2015-01-07",
        "type": "normal",
        "campus": "Sint-Annaplein",
        "open": "08:15:00",
        "close": "12:30:00"
    },
    {
        "datum": "2015-01-07",
        "type": "normal",
        "campus": "Sint-Annaplein",
        "open": "13:30:00",
        "close": "17:30:00"
    }
]

我可能忘记了检查字段是否在数组中之前的一个重要步骤,但我对此很陌生,所以我没有找到它。

明确地说,我需要一个包含每个独特校园的数组,例如

['Kantienberg','Kattenberg','Mariakerke','Sint-Amandsberg','Sint-Annaplein']

但我明白了

["kantienberg", "kattenberg", "mariakerke", "sint-amandsberg", "sint-amandsberg", "sint-annaplein", "sint-annaplein"]

$.inArray() returns -1 如果数组中不存在该值。当用作布尔值时,-1 等同于 true。您需要更改条件以明确检查 -1:

if ($.inArray(ca, camp) == -1) {
    camp.push(ca);  
}

Example fiddle

我的解决方案如下:

var camp = {};
$.each(data, function(i, field) {
    var dt = field['datum'];
    var openClose = field['open'] + "-" + field['close'];
    var ca = field['campus'];
    if (camp[ca]) {
        camp[ca].push(openClose);
    } else {
        camp[ca] = [openClose];
    }
});

这会生成一个将校园名称映射到开放时间的对象,如下所示:

"name" -> ["open-close", "open-close", ...]

创建对象后,我们只需将其附加到 DOM:

for (prop in camp) {
    $("#wtf").append(prop + " ");
    for (var i = 0; i < camp[prop].length; i++) {
        $('#wtf').append(camp[prop][i]);
        if (i !== camp[prop].length - 1) {
            $('#wtf').append(" ");
        }
    }
    $('#wtf').append('<br>');
}

我认为这种方法更适合你的原因是,这样所有的打开和关闭时间都被保留了。在另一种方法中,将只保留其中一个间隔。

fiddle is located here.