赞
踩
- //【递归】说明:递归方法清空多维数组中的指定元素为对应值(对象)
- let clearNullElementsInArrayByProps = (arr, {
- props = {
- fieldName: `removed`,
- fieldValue: true,
- },
- } = {}) => {
- return (arr || []).filter(v => {
- if (v[props.fieldName] === props.fieldValue) {
- return false;
- } else {
- if (v.children) {
- v.children = clearNullElementsInArrayByProps (v.children, { props });
- return v.children.length > 0
- } else {
- return true;
- }
- }
- });
- }
- clearNullElementsInArrayByProps (数组或对象);
-
- let arr = [
- {
- label: '显示文本1', children: [
- { label: '显示文本1-1', removed: true, },
- { label: '显示文本1-2', removed: 0, },
- { label: '显示文本1-3', removed: true, },
- { label: '显示文本1-4', removed: false, },
- { label: '显示文本1-5', },
- ],
- },
- {
- label: '显示文本1', removed: true,
- }
- ]
- clearNullElementsInArrayByProps (arr);
-
- /*
- 输出:
- [
- {
- "label": "显示文本1",
- "children": [
- {
- "label": "显示文本1-2",
- "removed": 0
- },
- {
- "label": "显示文本1-4",
- "removed": false
- },
- {
- "label": "显示文本1-5"
- }
- ]
- }
- ]
- */
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。