1

I'm trying to create an array in javascript with unique elements from a JSON feed. Maybe I think to much in php but there a simple script as below works.

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

In JS it doesn't:

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

It just adds every field['needed'] to this.

Full code so you know how I get my 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 looks like:

[
    {
        "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"
    }
]

I'm probably forgetting an important step before checking if the field is in the array, but I'm quite new to this, so I don't find it.

To be clear, I need an array with every unique campus like

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

but I get

["kantienberg", "kattenberg", "mariakerke", "sint-amandsberg", "sint-amandsberg", "sint-annaplein", "sint-annaplein"]
14
  • 4
    this is a reserved keyword, you should not attach variables to it Commented Jan 7, 2015 at 13:23
  • 1
    inArray returns -1 if not found api.jquery.com/jquery.inarray. So it is still true. You have to check for >-1. Commented Jan 7, 2015 at 13:27
  • @devqon I know, I just used it in my examples. In my working code I use other, not-reserved names. Commented Jan 7, 2015 at 13:28
  • hehe, student at Ghent ? Commented Jan 7, 2015 at 13:29
  • 1
    Please check again jsfiddle.net/dr1bxwqf/2, I've updated it Commented Jan 7, 2015 at 14:10

2 Answers 2

2

$.inArray() returns -1 if the value does not exist in the array. When used as a boolean, -1 equates to true. You need to change your condition to explicitly check for -1:

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

Example fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

My solution is the following:

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

This yields an object which maps the campus names to the opening times, like this:

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

After the object is created, we just append it to the 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>');
}

The reason why I think this method is preferable for you, is that, this way all the opening and closing times are conserved. In the other approach, only one of these intervals will remain.

The fiddle is located here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.