0

Extending a previous question about JavaScript and jQuery, I'm trying to make an array and then look into it, but the array contains dimensions and I can't get it right..

var markers = [];
$.getJSON('GetList', "id"= + data.id,
        function(data){
           $.each(data.list, function(index, data) {
                }
                markers.push( {
                    category:data.category,
                    id:data.id,
                    location:{
                        latitude:data.location.latitude,
                        longitude:data.location.longitude
                    }
                });
            });
        });
        return markers;
}

The first thing that strikes me is that every item will now be called "Object", while default, I'm still wondering if they can be labeled instead?

Further down the line, I try to access the id to compare it to a selection by the user, but it doesn't work.

    var selection = $(div_id).val();
    var arr = $.inArray(selection, markersArray.id);
    if( arr >= 0) {
        return search(selection, div_id);
    } else {
        throw("selection not found in selectionSearch()");
    }

What am I doing wrong here..?

2 Answers 2

1

To label the objects, add a toString function, like this:

            markers.push({
                toString: function () { return this.something; },
                category:data.category,
                id:data.id,
                location:{
                    latitude:data.location.latitude,
                    longitude:data.location.longitude
                }
            });

To search the array, you'll need your own loop.
jQuery's $.inArray function searches the array for an exact match; it cannot be used to find an object with a matching property.
You can find a match yourself, like this:

for(var i = 0; i < markers.length; i++) {
    if (markers[i].id === selection)
        return search(selection, div_id);
}
throw("selection not found in selectionSearch()");
Sign up to request clarification or add additional context in comments.

2 Comments

First of all - the toString example contains faults (rogue parentheses?) Second - shouldn't it be possible to just label the objects after data.id and find it with $.inArray?
Well, it works like you did it so, no arguments there. However I ran into "a is null" with it which is ... such a great error...
0

The type of each item is Object, there is nothing strange about that. There is no reason to try to change that.

To filter out items from an array based on a property in the item, you use the grep method:

var arr = $.grep(markers, function(e){ return e.id === selection; });

To check if the array is empty, you don't compare the array to a number, you use the length property:

if (arr.length > 0) {

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.