根据某些属性过滤嵌套数组

我有一个数组:


var data = [{

  "name": "Main Hobbies",

  "checked": false,

  "children": [{

    "name": "Dance",

    "checked": false,

    "children": [{

      "name": "Salsa",

      "checked": true

    }, {

      "name": "Solo",

      "checked": false

    }]

  }, {

    "name": "Sports",

    "checked": false,

    "children": [{

      "name": "Cricket",

      "checked": true

    }]

  }]

}, {

  "name": "Game",

  "checked": false,

  "children": [{

    "name": "PUBG",

    "checked": false

  }, {

    "name": "Cricket",

    "checked": false

  }, {

    "name": "Football",

    "checked": true

  }]

}]


function removeFalseElem(arr) {

  arr.forEach(element => {

    if (Array.isArray(element.children) && element.children.length > 0) {

      element.children = element.children.filter(e => {

        return e.checked === true

      });

      element.children.map(e => {

        delete e['id'], delete e['checked']

      })

    } else {

      removeFalseElem(element.children);

    }

    delete element['id'], delete element['checked'];

  });


}

removeFalseElem(data);

console.log(data);


我如何映射name到key和包含 where is . 应该包含,因为它已被检查。我只想颠倒这个问题的方法childrenarraycheckedtrueMain hobiesSalsa


输出应该像


{

  "Main Hobbies": { Dance: ["Salsa"], Sports: ["Cricket"] },

  Game: ["Football"]

}

必须只保留checked:true值


30秒到达战场
浏览 115回答 2
2回答

繁星coding

使用以下辅助方法,遍历项目并构建所需的输出。1) 过滤器——给定一个数组,返回为真的名称2) hasGrandChild——给定对象是否有子对象,子对象是否有子对象。const process = data => {  const filter = arr => arr.filter(x => x.checked).map(x => x.name);  const hasGrandChild = arr => arr.length > 0 && "children" in arr[0];  const output = {};  data.forEach(({ name, children }) => {        if (hasGrandChild(children)) {      const obj = {};      children.forEach(ch => (obj[ch.name] = filter(ch.children)));      output[name] = obj;    } else {      output[name] = filter(children);    }  });  return output;};var data = [  {    name: "Main Hobbies",    checked: false,    children: [      {        name: "Dance",        checked: false,        children: [          {            name: "Salsa",            checked: true          },          {            name: "Solo",            checked: false          }        ]      },      {        name: "Sports",        checked: false,        children: [          {            name: "Cricket",            checked: true          }        ]      }    ]  },  {    name: "Game",    checked: false,    children: [      {        name: "PUBG",        checked: false      },      {        name: "Cricket",        checked: false      },      {        name: "Football",        checked: true      }    ]  }];console.log(process(data));

慕容3067478

您可以检查子项是否不包含子项并返回一个名称数组或使用选中的节点创建一个对象。const    getObject = array => array.every(({ children }) => !children)        ? array.reduce((r, { name, checked }) => checked ? [...(r || []), name] : r, undefined)        : array.reduce((r, { name, checked, children = [] }) => {            children = getObject(children);            if (checked || children) r[name] = children;            return r;        }, {}),                data = [{ name: "Main Hobbies", checked: false, children: [{ name: "Dance", checked: false, children: [{ name: "Salsa", checked: true }, { name: "Solo", checked: false }] }, { name: "Sports", checked: false, children: [{ name: "Cricket", checked: true }] }] }, { name: "Game", checked: false, children: [{ name: "PUBG", checked: false }, { name: "Cricket", checked: false }, { name: "Football", checked: true }] }],    result = getObject(data);console.log(result);.as-console-wrapper { max-height: 100% !important; top: 0; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript