-2

Using the following sample data, I am attempting remove object entries where the array children is empty.

So based on allData below, the following would be removed alone:

{
    "name": "Tom",
    "info": "Tom info",
    "section_id": 3,
    "children": []
}

Since "children": [] is empty.

I have tried the following below but unsure how to achieve my expected result and target the specific object entry.

Based on comments below, a recursive solution is required but I am not sure how to do this.

let allData = {
            "name": "Max",
            "info": "Max info",
            "section_id": 1,
            "children": [
                {
                    "childName": "Sam",
                    "childAge": 5
                },
                {
                    "name": "Helen",
                    "info": "Helen info",
                    "section_id": 2,
                    "children": [
                        {
                            "childName": "Sarah",
                            "childAge": 11
                        },
                        {
                            "name": "Tom",
                            "info": "Tom info",
                            "section_id": 3,
                            "children": []
                        }
                    ]
                }
            ]
        }

let children = allData.children
const myData = children.filter(v => !v.children || v.children.length > 0)
myData.forEach((element, index) => {
  if (element) {
    console.log(element)
  }
});

The following console log is produced:

{
  "childName": 'Sam', 
  "childAge": 5
}

{
   "name": "Helen",
   "info": "Helen info",
   "section_id": 2,
   "children": [
     {
       "childName": "Sarah",
       "childAge": 11
     },
     {
       "name": "Tom",
       "info": "Tom info",
       "section_id": 3,
       "children": []
     }
   ]
}

I was looking at using the index to splice the array in order to remove:

 {
   "name": "Tom",
   "info": "Tom info",
   "section_id": 3,
   "children": []
 }

Any help would be great.

ArthurJ
  • 777
  • 1
  • 14
  • 39
  • 3
    _"Without the use of recursion"_ Why? – Cerbrus Dec 05 '22 at 08:13
  • 1
    If the data does not have a fixed depth, there's no other way than to use recursion. – Harun Yilmaz Dec 05 '22 at 08:14
  • I think I will need recursion as I assumed I didn't need it. I will update my question to reflect this. – ArthurJ Dec 05 '22 at 08:32
  • 2
    @HarunYilmaz you can always turn recursion into iteration https://stackoverflow.com/questions/931762/can-every-recursion-be-converted-into-iteration – Andrew Parks Dec 05 '22 at 08:33
  • 1
    If you want to remove all elements that have no children _recursively_, then `allData = {}` will be the correct answer. Think about it. – gog Dec 05 '22 at 08:39
  • 1
    A recursive solution was already provided on your [previous question](https://stackoverflow.com/questions/74621979/require-a-means-of-iterating-through-an-object-and-removing-the-whole-block-obje). What's wrong with it? – Robby Cornelissen Dec 05 '22 at 08:51
  • @AndrewParks You're right. I need to change my phrasing from `no other way` to something else. – Harun Yilmaz Dec 05 '22 at 08:55
  • 2
    Yea, don't repost questions like that... – Cerbrus Dec 05 '22 at 08:56

1 Answers1

0

const data = {"name":"Max","info":"Max info","section_id":1,"children":[{"childName":"Sam","childAge":5},{"name":"Helen","info":"Helen info","section_id":2,"children":[{"childName":"Sarah","childAge":11},{"name":"Tom","info":"Tom info","section_id":3,"children":[]}]}]}

let a = [[data, null, 0]] // array of [item, parent, index]
                          // top level has null parent

while(a.length) {
  let [item, parent, index] = a.pop()
  if(item.children) {
    if(item.children.length)
      item.children.forEach((child,index)=>a.push([child, item, index]))
    else
      parent.children.splice(index, 1)
  }
}
console.log(data)
Andrew Parks
  • 6,358
  • 2
  • 12
  • 27
  • To quote Harun: _"What if the data has 1 more layer? You are now hardcoding the depth based on the example data."_ – Cerbrus Dec 05 '22 at 08:45
  • @Cerbrus I'm not hardcoding depth. This solution works with any depth. The array `a` is used as a stack to allow us to deal with all objects at any depth – Andrew Parks Dec 05 '22 at 08:46
  • Hm, I see. That's some convoluted code to work around that recursion restriction (that turned out not to be a restriction after all) – Cerbrus Dec 05 '22 at 08:49
  • @Cerbrus it's the standard CompSci way: use a stack to convert recursion into a list of tasks that can be iterated over – Andrew Parks Dec 05 '22 at 08:49
  • However, in practice, we usually just turn to recursion.... – Cerbrus Dec 05 '22 at 08:51
  • @Cerbrus yes, that would certainly result in simpler code, if the original question allowed recursion – Andrew Parks Dec 05 '22 at 08:52
  • It does, OP realized the restriction was incorrect :D – Cerbrus Dec 05 '22 at 08:55