js递归循环

2020-01-15  本文已影响0人  都江堰古巨基

以前一直以为递归循环不能改变被递归的数据本身,但今天无意间试了试,发现可以改变数据本身,究其原因递归本身是一种指针的传递,指针指向的值改变了就会改变本身的值。
话不多说,上例子:
先是JS的例子:

// 数据如下:
var data = [{
     name: "所有物品",
     children: [
         {
             name: "水果",
             children: [{name: "苹果", children: [{name: '青苹果'}, {name: '红苹果'}]}]
         },
         {
             name: '主食',
             children: [
                 {name: "米饭", children: [{name: '北方米饭'}, {name: '南方米饭'}]}
             ]
         },
         {
             name: '生活用品',
             children: [
                 {name: "电脑类", children: [{name: '联想电脑'}, {name: '苹果电脑'}]},
                 {name: "工具类", children: [{name: "锄头"}, {name: "锤子"}]},
                 {name: "生活用品", children: [{name: "洗发水"}, {name: "沐浴露"}]}
             ]
         }
      ]
}]

// 通过下面的程序改变它:
const change = function(list){
    list.forEach(function(row){
        if(row.children){
            getStr(row.children)
        }else {
            console.log(row.name)
            row.name = "我要被改变了!!!"
        }
    })
}

// 运行后的data:
[
    {
        "name": "所有物品",
        "children": [
            {
                "name": "水果",
                "children": [
                    {
                        "name": "苹果",
                        "children": [
                            {
                                "name": "我要被改变了!!!"
                            },
                            {
                                "name": "我要被改变了!!!"
                            }
                        ]
                    }
                ]
            },
            {
                "name": "主食",
                "children": [
                    {
                        "name": "米饭",
                        "children": [
                            {
                                "name": "我要被改变了!!!"
                            },
                            {
                                "name": "我要被改变了!!!"
                            }
                        ]
                    }
                ]
            },
            {
                "name": "生活用品",
                "children": [
                    {
                        "name": "电脑类",
                        "children": [
                            {
                                "name": "我要被改变了!!!"
                            },
                            {
                                "name": "我要被改变了!!!"
                            }
                        ]
                    },
                    {
                        "name": "工具类",
                        "children": [
                            {
                                "name": "我要被改变了!!!"
                            },
                            {
                                "name": "我要被改变了!!!"
                            }
                        ]
                    },
                    {
                        "name": "生活用品",
                        "children": [
                            {
                                "name": "我要被改变了!!!"
                            },
                            {
                                "name": "我要被改变了!!!"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

dart 的demo:

main() {
  var data = [
    {
        "name": "所有物品",
        "children": [
            {
                "name": "水果",
                "children": [
                    {
                        "name": "苹果",
                        "children": [
                            {
                                "name": "我要被改变了!!!"
                            },
                            {
                                "name": "我要被改变了!!!"
                            }
                        ]
                    }
                ]
            },
            {
                "name": "主食",
                "children": [
                    {
                        "name": "米饭",
                        "children": [
                            {
                                "name": "我要被改变了!!!"
                            },
                            {
                                "name": "我要被改变了!!!"
                            }
                        ]
                    }
                ]
            },
            {
                "name": "生活用品",
                "children": [
                    {
                        "name": "电脑类",
                        "children": [
                            {
                                "name": "我要被改变了!!!"
                            },
                            {
                                "name": "我要被改变了!!!"
                            }
                        ]
                    },
                    {
                        "name": "工具类",
                        "children": [
                            {
                                "name": "我要被改变了!!!"
                            },
                            {
                                "name": "我要被改变了!!!"
                            }
                        ]
                    },
                    {
                        "name": "生活用品",
                        "children": [
                            {
                                "name": "我要被改变了!!!"
                            },
                            {
                                "name": "我要被改变了!!!"
                            }
                        ]
                    }
                ]
            }
        ]
    }
  ];
  change(data);
  print(data);
}

void change(List listData) {
  listData.forEach((e) {
    if (e["children"] != null)
      change(e["children"]);
    else
      e["name"] = "这是Dart的demo";
  });
}

//  输出:
[{name: 所有物品, children: [{name: 水果, children: [{name: 苹果, children: [{name: 这是Dart的demo}, {name: 这是Dart的demo}]}]}, {name: 主食, children: [{name: 米饭, children: [{name: 这是Dart的demo}, {name: 这是Dart的demo}]}]}, {name: 生活用品, children: [{name: 电脑类, children: [{name: 这是Dart的demo}, {name: 这是Dart的demo}]}, {name: 工具类, children: [{name: 这是Dart的demo}, {name: 这是Dart的demo}]}, {name: 生活用品, children: [{name: 这是Dart的demo}, {name: 这是Dart的demo}]}]}]}]

上一篇 下一篇

猜你喜欢

热点阅读