0

I'm having some trouble creating a multidimensional array where im putting some counties and places that are in the county.

Example of what im trying to do:

[
    'HEDMARK' => [
        1 => 'ELVERUM',
        2 => 'HAMAR 
    ],
    'OSLO' => [
        1 => 'OSLO'
    ]
]

The code im using is this:

    var $query_place = [];

    // Get each checked county
    $.each($('.county input:checkbox:checked'), function () {
        $query_place.push({
            county: $(this).val(),
            postal: []
        });
    });

    // Get each checked postal
    $.each($('.postal input:checkbox:checked'), function() {
        $query_place[$(this).attr('data-county')].postal.push();
    });

The error im getting in the console is this:

TypeError: $query_place[$(...).attr(...)] is undefined

Is there something im forgetting here? Or have i just done this wrong?

the checkboxes

5
  • Do you specifically need an array of objects? A single object would seem to be a much more simple and effective solution here. Commented Sep 21, 2015 at 9:44
  • Use $().data( attributeName ) better, and mind the countytypo perhaps ^^ Commented Sep 21, 2015 at 9:46
  • Look in your browser dev tool (firebug in firefox for instance) to see what you really have in your array, it could help. Commented Sep 21, 2015 at 9:49
  • Yea, I need an array of objects. So that each county, gets its own array of postals. Tried using $().data instead, but I get the same error. I tried to switch the the inside of $.each postal line to alert($(this).val()); but it doesn't alert anything, so i think i can't use $(this) for some reason. Commented Sep 21, 2015 at 10:02
  • I know got each county to work right (imgur.com/wlZYu5p). I only need know to get each postal pushed in to the array where it belongs. How would i do that? I tried the following, but it gives me an error (TypeError: $query_place.county is undefined) $query_place.county[$(this).attr('data-county')].postal.push($(this).val()); Commented Sep 21, 2015 at 10:46

2 Answers 2

1

You have to implement your code like this

var $query_place = [];    
    // Get each checked county
    $.each($('input:checkbox.county:checked'), function () {
        $query_place.push({
            county: $(this).val(),
            postal: setPostal($(this).val())
        });        
    });           

    function setPostal(county)
    {
        var postal = [];
        // Get each checked postal
        $.each($('input:checkbox.postal:checked'), function () {
            if ($(this).attr('data-county') == county)
                postal.push($(this).val());
        });
        return postal;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

you have to parse the $query_place array object before pushing any values into it.

$.each($('.county input:checkbox:checked'), function () {
var values={};
values.county= $(this).val();
values.postal=[];
var a=JSON.parse($query_place);
a.push(value);

})

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.