Arthur Yakovlev
Arthur Yakovlev

Reputation: 9309

Error on second step delete of object item

   $("body").on("click", ".delete_human", function(){
        var custom_tdid = $(this).attr('data-id');
        $(".peoples_data .xls-people-"+custom_tdid).hide(400 , function() {$(".peoples_data .xls-people-"+custom_tdid).remove()});
        console.log(people);
        for(var i = 0; i < people.length; i++){
            if(people[i].tdid==custom_tdid)
            {
                delete people[i];
                delete people_to_parse[i];
                delete people_to_save_parse[i];
            }
        }
        console.log(people); 
    });

Morkup:

        var td1 = "<tr class='xls-people-"+custom_tdid+"'><td><div class='parse_name Photo-text-"+custom_tdid+"'><img src='https://graph.facebook.com/3/picture'/></div></td>";
        var td2 = "<td><div class='parse_name ID-text-"+custom_tdid+"'>"+custom_id+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='ID-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td3 = "<td><div class='parse_name Name-text-"+custom_tdid+"'>"+custom_firstname+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='Name-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td4 = "<td><div class='parse_name Surname-text-"+custom_tdid+"'>"+custom_lastname+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='Surname-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td5 = "<td><div class='parse_name Nickname-text-"+custom_tdid+"'>"+custom_nickname+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='Nickname-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td6 = "<td><div class='parse_name Phone-text-"+custom_tdid+"'>"+custom_phone+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='Phone-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td7 = "<td><div class='parse_name City-text-"+custom_tdid+"'>"+custom_city+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='City-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td8 = "<td><div class='parse_name CountFriends-text-"+custom_tdid+"'>"+custom_countfriends+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='CountFriends-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td9 = "<td><div class='parse_name Mail-text-"+custom_tdid+"'>"+custom_mail+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='Mail-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td10 = "<td><div class='parse_name Avocation-text-"+custom_tdid+"'>"+custom_avocation+"</div><div class='parse_name1'><div class='switch demo3'><input type='checkbox' id='counters' class='Avocation-"+custom_tdid+"'><label><i></i></label></div></div></td>";
        var td11 = "<td><div class='parse_name Status-text-"+custom_tdid+"'>Wait to parse</div><button class='delete_human' data-id='"+custom_tdid+"'>DELETE</button></td></tr>";
        $(".peoples_data tbody").append(td1+td2+td3+td4+td5+td6+td7+td8+td9+td10+td11).hide().show('slow');

enter image description here

Upvotes: 1

Views: 30

Answers (2)

code-jaff
code-jaff

Reputation: 9330

the issue is removing the element by delete operator, use splice instead, since delete still keeps the index with the value undefined, but splice actually removes the element from the array

$("body").on("click", ".delete_human", function(){
    var custom_tdid = $(this).attr('data-id');
    $(".peoples_data .xls-people-"+custom_tdid).hide(400 , function() {$(".peoples_data .xls-people-"+custom_tdid).remove()});
    console.log(people);
    for(var i = 0; i < people.length; i++){
        if(people[i].tdid==custom_tdid)
        {
            people.splice(i, 1);
            people_to_parse.splice(i, 1);
            people_to_save_parse.splice(i, 1);
        }
    }
    console.log(people); 
});

Debug

You can see ( in your console output ) there is actually two elements in the array, but length still holding the value of 3, now you can realize where the error happens.

Upvotes: 1

tpdietz
tpdietz

Reputation: 1368

Your for loop's constraint is people.length. There is no guarantee that people_to_parse and people_to_save are the same length as people.

Upvotes: 0

Related Questions