RozzA
RozzA

Reputation: 609

jquery .each() incorrectly updating object

I have the following code, basically, an object in an object in an object.

alertQ = {
    0: {
        French: {
            start: "1am",
            duration: "1hr",
            status: "queued"
        }
    },
    1: {
        Italian: {
            start: "2am",
            duration: "2hr",
            status: "queued"
        }
    },
    2: {
        // ...etc...
    }
};

function sendTrades(){
    var trades = $J("#queue_listbox input:checked");
    $J.each(trades, function(key, val){ //iterate thru any selected option
        var id = val.value; //checkbox value contains object "id"
        $J.each(alertQ[id], function(key, val){
            Dbug(key);
            alertQ[id][key].status = "active";
            //val.status = "active"; //this was also tried with same result
        });
    });
    drawTrades(); //utility function. read only. creates the <input> checkbox elements for display, and is fired before this function to initialize the list
}

I can correctly navigate to the innermost object and read the variables from there. The whole alertQ object is constructed with jQuery .each() loops in basically the same way sendTrades() is attempting to modify it.
It builds perfectly & is accessible from console.
sendTrades() is fired upon a button click.
The problem arises when I select one from a list greater than 1, let's assume I've selected French:
Dbug(key); fires correctly, and names French only. Dbug simply passes data to console.log()
ALL status values are changed to "active" << should not happen

I have tried selecting all options and console logging indicates correctly all names, and all status are changed to "active" as it should.
But why does it change them all when it should only change the selected ones as mentioned above?

sample HTML below

<input value="0" type="checkbox">French
<input value="1" type="checkbox">Italian
<input value="2" type="checkbox">...etc...

If need be, the 3rd-nested object could be merged into the 2nd-nested object.

Upvotes: 1

Views: 544

Answers (1)

SSA
SSA

Reputation: 5493

I believe it's not like you are setting all to active but in your tests, you'r are not resetting alertQ object again. So first time you select all and all status are set to "active", in second test, you only check one box and it's setting that one to active but previous ones are already set to active.

Here is example: http://jsbin.com/vonumiqi/1/

//You need to reset the non active to queued or other status you want.

var tradesUnChecked = $("#check1 :not('input:checked')");
$.each(tradesUnChecked, function(key, val){ //iterate thru any selected option
    var id = val.value; //checkbox value contains object "id"
     console.log(alertQ[id]);
    $.each(alertQ[id], function(key, val){

        console.log(key);
        alertQ[id][key].status = "queued";
        //val.status = "active"; //this was also tried with same result
    });
});

Upvotes: 1

Related Questions