应用地图功能有效,而foreach函数不起作用

时间:2019-03-09 20:40:43

标签: javascript json reactjs ecmascript-6 foreach

我有一个react应用,使用map函数显示嵌套json中的项目,但是其中一个类别可以为空,这会使整个应用崩溃,我已经使用js的每个函数测试了同一个json的foreach函数,并且工作得很好,我正在尝试转换解决空类别问题的相同逻辑。

这是我在同一个json上进行的每个测试的清单,但带有列表:

测试1

;
    if (chanName === 'usernameperson

测试2

let obj = {
  "name": "Menu",
  "children": [
    {
      "type": "category",
      "name": "Burgers",
      "children": [
        {
          "type": "item",
          "name": "Burger 1",
          "children": [
            {
              "type": "modifier",
              "name": "Promo",
              "children": [
                {
                  "type": "item",
                  "name": "Promo 1"
                }
              ]
            },
            {
              "type": "group",
              "name": "Drinks",
              "children": [
                {
                  "type": "item",
                  "name": "Coke"
                },
                {
                  "type": "item",
                  "name": "Light Coke"
                },
                {
                  "type": "item",
                  "name": "Sprite"
                },
                {
                  "type": "item",
                  "name": "Fanta"
                }
              ]
            },
            {
              "type": "modifier",
              "name": "Without",
              "children": [
                {
                  "type": "ingredient",
                  "name": "Onion"
                },
                {
                  "type": "ingredient",
                  "name": "Tomato"
                },
                {
                  "type": "ingredient",
                  "name": "Pickles"
                }
              ]
            }
          ]
        },
        {
          "type": "item",
          "name": "Burger 2",
          "children": [
            {
              "type": "modifier",
              "name": "Promo",
              "children": [
                {
                  "type": "item",
                  "name": "Promo 1"
                }
              ]
            },
            {
              "type": "group",
              "name": "Drinks",
              "children": [
                {
                  "type": "item",
                  "name": "Coke"
                },
                {
                  "type": "item",
                  "name": "Light Coke"
                },
                {
                  "type": "item",
                  "name": "Sprite"
                },
                {
                  "type": "item",
                  "name": "Fanta"
                }
              ]
            },
            {
              "type": "modifier",
              "name": "Without",
              "children": [
                {
                  "type": "ingredient",
                  "name": "Onion"
                },
                {
                  "type": "ingredient",
                  "name": "Tomato"
                },
                {
                  "type": "ingredient",
                  "name": "Pickles"
                }
              ]
            }
          ]
        },
        {
          "type": "item",
          "name": "Coming Soon Offers"
        }
      ]
    },
    {
      "type": "category",
      "name": "Pizzas",
      "children": [
        {
          "type": "item",
          "name": "Pizza 1",
          "children": [
            {
              "type": "modifier",
              "name": "Promo",
              "children": [
                {
                  "type": "item",
                  "name": "Promo 1"
                }
              ]
            },
            {
              "type": "group",
              "name": "Drinks",
              "children": [
                {
                  "type": "item",
                  "name": "Coke"
                },
                {
                  "type": "item",
                  "name": "Light Coke"
                },
                {
                  "type": "item",
                  "name": "Sprite"
                },
                {
                  "type": "item",
                  "name": "Fanta"
                }
              ]
            },
            {
              "type": "modifier",
              "name": "Without",
              "children": [
                {
                  "type": "ingredient",
                  "name": "Onion"
                },
                {
                  "type": "ingredient",
                  "name": "Mashrooms"
                },
                {
                  "type": "ingredient",
                  "name": "Olives"
                }
              ]
            }
          ]
        },
        {
          "type": "item",
          "name": "Pizza 2",
          "children": [
            {
              "type": "modifier",
              "name": "Promo",
              "children": [
                {
                  "type": "item",
                  "name": "Promo 1"
                }
              ]
            },
            {
              "type": "group",
              "name": "Drinks",
              "children": [
                {
                  "type": "item",
                  "name": "Coke"
                },
                {
                  "type": "item",
                  "name": "Light Coke"
                },
                {
                  "type": "item",
                  "name": "Sprite"
                },
                {
                  "type": "item",
                  "name": "Fanta"
                }
              ]
            },
            {
              "type": "modifier",
              "name": "Without",
              "children": [
                {
                  "type": "ingredient",
                  "name": "Onion"
                },
                {
                  "type": "ingredient",
                  "name": "Mashrooms"
                },
                {
                  "type": "ingredient",
                  "name": "Olives"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}
let body = document.querySelector('body');
function print(obj){
  let str = `<li>${obj.name}</li>`;
  if(obj.children){
    str += '<ul>' 
    for(let c of obj.children) str += print(c)
    str += '</ul>'
  }
  return str;
}
document.body.innerHTML = print(obj);

1 个答案:

答案 0 :(得分:1)

您在多个地方都缺少空检查对象。

  1. ItemList组件的render函数中,有一个else语句,您在其中分配selectedChild和selectedItem。您需要检查activelist.childrenselectedChild.children是否为空
   const selectedChild = activelist.children.length ? activelist.children[this.state.selected] : null;
   const selectedItem = selectedChild.children && selectedChild.children.length
        ? selectedChild.children[this.state.itemSelected]: null;
  1. 要渲染selectedChild,您应该检查child是否为空。
{
  selectedChild &&
  selectedChild.children &&
  selectedChild.children.length &&
  selectedChild.children.map(
    (item, index) => (
      <Item
        className="person"
        key={index}
        Title={item.name}
        onClick={this.handleClick}
        index={index}
      />
   )
)}
  1. 要渲染selectedItem,您应该检查子级是否为空
<div>
  { selectedItem &&
    selectedItem.children &&
    selectedItem.children.map((item, index) => (
      <Modifiers
        key={index}
        title={item.name}
        myKey={index}
        options={item.children}
        childk={item.id}
       />
     ))
  }
</div>
<div>
  { 
    selectedItem &&
    selectedItem.size &&
    (<div>
        <Size
           options={selectedItem.size}
           sizetitle={"Size"}
        />
       </div>)
   }
</div>