There is an object within which other objects are accessible by keys:

segment0_0 segment0_1 segment1_0 segment1_1 

I need to delete these elements from the object in the loop, because there can also be elements with keys segment2_0 , segment3_1 , etc. I tried to delete them in a nested loop, but for some reason the last element under the key segment1_1 never deleted:

 for(let i = 0; i < Object.keys(rules).length; i++) { for(let j = 0; j < Object.keys(rules).length; j++) { if(rules[`airports_seg${i}_${j}`]) { delete rules[`airports_seg${i}_${j}`]; } } } 

How do I properly remove such items?

    2 answers 2

    Suppose there is an object

     var rules = { segment0_0:1, segment0_1:'3232', segment1_0:{}, segment1_1:true, segment2_1:true, } 

    The key of an object consists for example of a common attribute, type and id, those in segment0_1 are a segment-general attribute, 0 is a type, 1 is an id. task to remove types less than 2

     var separator = 'segment'; for(let key in rules) { if(key.indexOf(separator)>-1) { let info = key.replace(separator,''); let info_arr = info.split('_'); let type = info_arr[0]; let id = info_arr[1]; if(type<2) { delete rules[key]; } } } 

    I apologize if I came up with too much, you do not have an exact description of the task, I had to think it out myself. In any case, I think it helped. What you did was very wrong, so it didn't work.

    • Thanks, works :) - JamesJGoodwin
     delete myObject.prop; // или, delete myObject['prop']; // или, var prop = "prop"; delete myObject[prop]; 

    Test:

     var myObject = { "prop1": "HELLO WORLD", }; delete myObject.prop1; 

    For your question:

    You have the keys of segment0_0 and you are trying to delete airports_seg0_0 + in your second cycle, the element is deleted and your cycle simply does not reach 1_1

     var rules = { segment0_0: "asd", segment0_1: "asd", segment1_0: "asd", segment1_1: "asd", } var a = { ...rules }; for(let i = 0; i < Object.keys(rules).length; i++) { for(let j = 0; j < Object.keys(a).length; j++) { if(rules[`segment${i}_${j}`]) { delete rules[`segment${i}_${j}`]; } } } console.log(rules); 

    another option:

     var rules = { segment0_0: "asd", segment0_1: "asd", segment1_0: "asd", segment1_1: "asd", } var arr = []; for(let i = 0; i < Object.keys(rules).length; i++) { for(let j = 0; j < Object.keys(rules).length; j++) { console.log(i,j); if(rules[`segment${i}_${j}`]) { arr.push(`segment${i}_${j}`) } } } for(let i=0; i< arr.length; i++) { console.log(arr[i]) delete rules[arr[i]]; } 
    • one
      "but for some reason the last element under the key segment1_1 is never deleted" - you don’t delete the same =) I think "You have the keys segment0_0 and you are trying to delete airports_seg0_0" that he just didn’t fix his code for example, and the error was not in this, for if in this, then nothing would be removed from him, not just segment1_1 - Geri4
    • one
      Well, then it is even simpler, the fact is that an element with index 1 is deleted from it at the first iteration) it simply needs to copy the object and pass the second cycle. I fixed it so you can check :) - Jamshed