JS recursive tree selects the specified object

  • requirements: recursively unchecked objects
  • expectation: the object of checked: false cannot be selected, for example, in the following data (watermelon, pudding, tasteless)
  • data
[
   {
          title: "parent 1",
          expand: true,
          checked: false,
          children: [
            {
              title: "parent 1-1",
              expand: true,
              checked: false,
              children: [
                {
                  checked: false,
                  title: ""
                },
                {
                  checked: false,
                  title: "leaf 1-1-2"
                }
              ]
            },
            {
              title: "parent 1-2",

              checked: false,
              expand: true,
              children: [
                {
                  checked: false,
                  title: "leaf 1-2-1"
                },
                {
                  checked: false,
                  title: "leaf 1-2-1"
                }
              ]
            }
          ]
        },
        {
          title: "parent 11",
          checked: false,
          expand: true,
          children: [
            {
              title: "parent 11-1",
              checked: false,
              expand: true,
              children: [
                {
                  checked: false,
                  title: "leaf 11-1-1"
                },
                {
                  checked: false,
                  title: "leaf 11-1-2"
                }
              ]
            },
            {
              title: "adfasdfas",
              checked: false,
              expand: true,
              children: [
                {
                  checked: false,
                  title: ""
                },
                {
                  checked: false,
                  title: ""
                }
              ]
            }
          ]
        }
]

implementation method:

    function findTarget(source, targetName) {
            if (source && source.length) {
              for (let item of source) {
                if (item.checked === targetName) {
                  console.log(item.title);
                } else if (item.children && item.children.length) {
                  findTarget(item.children, targetName);
                }
              }
            }
          }
          findTarget(this.data2, false);
          for (let i = 0; i < this.data2.length; iPP) {
            findTarget(this.data2[i], false);
          }
        }

problem: unable to achieve the desired effect


I don't know if it's what you want!

var data = [{

        "title": "parent 1",
        "expand": true,
        "checked": false,
        "children": [{
                "title": "parent 1-1",
                "expand": true,
                "checked": false,
                "children": [{
                        "checked": true,
                        "title": "leaf 1-1-1"
                    },
                    {
                        "checked": false,
                        "title": "leaf 1-1-2"
                    }
                ]
            },
            {
                "title": "parent 1-2",
                "checked": false,
                "expand": true,
                "children": [{
                        "checked": false,
                        "title": "leaf 1-2-1"
                    },
                    {
                        "checked": false,
                        "title": "leaf 1-2-1"
                    }
                ]
            }
        ]
    },
    {

        "title": "parent 11",
        "checked": false,
        "expand": true,
        "children": [{
            "title": "parent 11-1",
            "checked": true,
            "expand": true,
            "children": [{
                    "checked": true,
                    "title": "leaf 11-1-1"
                },
                {
                    "checked": true,
                    "title": "leaf 11-1-2"
                }
            ]
        }]
    }
];


let listArr = [];
const settle = (data) => {
    data.forEach(v => {
        listArr.push({
            label: v.title,
            expand: v.expand || false,
            checked:v.checked
        });
        if (v.children && v.children.length > 0) {
            settle(v.children,)
        }
    });
};
settle(data);
console.log(listArr);

clipboard.png

//


clipboard.png


.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e50aab-43bab.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e50aab-43bab.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?