簡體   English   中英

如果不在數組 - > array.push

[英]if not in array -> array.push

我正在嘗試使用JSON提要中的唯一元素在javascript中創建一個數組。 也許我想在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);
    }
}

它只是this添加了每個field['needed']

完整代碼,以便您了解我如何獲得我的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()將返回-1 當用作布爾值時, -1等於true 您需要更改條件以顯式檢查-1

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

示例小提琴

我的解決方案如下:

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>');
}

我之所以認為這種方法更適合您,是因為這樣可以節省所有開啟和關閉時間。 在另一種方法中,只剩下其中一個間隔。

小提琴就在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM