3

Someone could help me with this problem? I have an object/array where I put an object with "status = true" after a specific condition. Now, my problem is, when all children objects are true, I would like to delete these objects and the parent. I will give an example of this issue.

function removeProp(obj, propToDelete,value) {
    for (var property in obj) {
            if (obj.hasOwnProperty(property)) {
                if (typeof obj[property] == "object") {
                    removeProp(obj[property],propToDelete,value);
                } else {
                if(property === propToDelete && obj[property] == value){
                    delete obj[property];
                    //delete obj;
                    }
                }
            }
     }
}

var obj = 
{
    "name":"Bank Branch 1",
    "requests":[
       {
          "date":"2019-10-16 03:18:02",
          "req":[
             {
                "amount":"300",
                "coin":2
             },
             {
                "amount":"500",
                "coin":5
             }
          ]
       },
       {
          "date":"2019-10-16 03:19:05",
          "req":[
             {
                "amount":"300",
                "coin":2,
                "status":true
             },
             {
                "amount":"500",
                "coin":5
             }
          ]
       },
       {
          "date":"2019-10-16 03:19:20",
          "req":[
             {
                "amount":"22",
                "coin":2,
                "status":true
             },
             {
                "amount":"111",
                "coin":5,
                "status":true
             }
          ]
       }
    ]
 }
console.log(JSON.stringify(obj));
removeProp(obj,"status",true);
console.log(JSON.stringify(obj));

I expect this one, where I have to remove all objects where the status was true, and the parent "date" too. Look for the "2019-10-16 03:19:05", I don't want to remove it because there is an object that there is no "status true" yet... And "2019-10-16 03:19:20" was removed because all objects were true.

var obj = 
{
    "name":"Bank Branch 1",
    "requests":[
       {
          "date":"2019-10-16 03:18:02",
          "req":[
             {
                "amount":"300",
                "coin":2
             },
             {
                "amount":"500",
                "coin":5
             }
          ]
       },
       {
          "date":"2019-10-16 03:19:05",
          "req":[
             {
                "amount":"300",
                "coin":2,
                "status":true
             },
             {
                "amount":"500",
                "coin":5
             }
          ]
       }
    ]
 }
Yaser Darzi
  • 1,480
  • 12
  • 24
Danilo
  • 381
  • 1
  • 2
  • 4
  • You need 2 loops here, 1 for looping the `obj.requests` array, and then another to loop the `req` array inside these. – Keith Oct 21 '19 at 14:15

3 Answers3

6

You can filter the results into another array using Array.filter() and Array.every() like this -

var result = obj.requests.filter(x => !x.req.every(y => y.status));

Run the snippet below to check results in the console.

var obj = 
{
    "name":"Bank Branch 1",
    "requests":[
       {
          "date":"2019-10-16 03:18:02",
          "req":[
             {
                "amount":"300",
                "coin":2
             },
             {
                "amount":"500",
                "coin":5
             }
          ]
       },
       {
          "date":"2019-10-16 03:19:05",
          "req":[
             {
                "amount":"300",
                "coin":2,
                "status":true
             },
             {
                "amount":"500",
                "coin":5
             }
          ]
       },
       {
          "date":"2019-10-16 03:19:20",
          "req":[
             {
                "amount":"22",
                "coin":2,
                "status":true
             },
             {
                "amount":"111",
                "coin":5,
                "status":true
             }
          ]
       }
    ]
 }

var result = obj.requests.filter(x => !x.req.every(y => y.status));

console.log(result);

And then simply replace the requests property with the newly created array.

obj.requests = result;
Vandesh
  • 6,368
  • 1
  • 26
  • 38
2

It's common nowadays when dealing with object & arrays to not mutate them. Things like React etc, work better when you do this. If this is the case @Vandesh answer is what you want.

On the other hand, if you still want to mutate your obj, you can do it like this ->

var obj = {"name":"Bank Branch 1","requests":[{"date":"2019-10-16 03:18:02","req":[{"amount":"300","coin":2},{"amount":"500","coin":5}]},{"date":"2019-10-16 03:19:05","req":[{"amount":"300","coin":2,"status":true},{"amount":"500","coin":5}]},{"date":"2019-10-16 03:19:20","req":[{"amount":"22","coin":2,"status":true},{"amount":"111","coin":5,"status":true}]}]};

obj.requests.forEach((f, ix) => {
  if (f.req.every(x => x.status)) 
    obj.requests.splice(ix, 1);
});

console.log(obj);
Keith
  • 22,005
  • 2
  • 27
  • 44
0

Your code looks to be going in the right direction. If you want to delete the object itself, it would be done using "delete object" and would be placed outside the loop (keep in mind the scope here since you are passing obj to the function). However, delete operator deletes only a reference and not the object itself. Here is the link for the doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

Also found a similar link on another post: Deleting Objects in JavaScript