将深度或父计数属性添加到嵌套的JSON数据

时间:2018-02-01 17:46:15

标签: javascript json recursion

我有一个嵌套位置的JSON结构,如下所示:(注意,这是伪json,而不是实际的json)

override func viewDidLoad() {

    super.viewDidLoad()
    self.navigationItem.title = "Login"
    self.view.backgroundColor = UIColor.blue

    //Mark:- making UITextFeild

    var UserName: UITextField {
        let tf = UITextField()
        tf.borderStyle = .none
        tf.layer.cornerRadius = 5
        tf.backgroundColor = UIColor()
        tf.textColor = UIColor(white: 0.9, alpha: 0.5)
        tf.font = UIFont.systemFont(ofSize: 17)
        tf.autocorrectionType = .no

        //Mark:- PlaceHolder

        var placeholder = NSMutableAttributedString()
        placeholder = NSMutableAttributedString(attributedString: NSMutableAttributedString(string: "UserName", attributes: [.font:UIFont.systemFont(ofSize: 18)]))
        tf.attributedPlaceholder = placeholder
        return tf
    }
}

我希望能够通过这些数据进行递归,并为每个项目添加一个属性,表示它在层次结构中的位置或深度,所以最后,我会有这样的事情:

location : "Location Parent"
locationid : 100
parent_location_id: ''

SUBLOCATIONS : Array(2)
    location : "Location Child"
    locationid : 200
    parent_location_id: 100

    location : "Location Child Two"
    locationid : 300
    parent_location_id: 100

location : "Location Parent Two"
locationid : 101
parent_location_id: ''

SUBLOCATIONS : Array(2)
    location : "Location Child"
    locationid : 201
    parent_location_id: 101

    location : "Location Child Two"
    locationid : 301
    parent_location_id: 101

    SUBLOCATIONS : Array(2)
        location : "Location Sub Child"
        locationid : 401
        parent_location_id: 301

        location : "Location Sub Child Two"
        locationid : 501
        parent_location_id: 301

我可以通过这样的结构递归:

location : "Location Parent"
locationid : 100
parent_location_id : ''
level: 1

SUBLOCATIONS : Array(2)
    location : "Location Child"
    locationid : 200
    parent_location_id : 100
    level: 2

    location : "Location Child Two"
    locationid : 300
    parent_location_id : 100
    level: 2

location : "Location Parent Two"
locationid : 101
parent_location_id : ''
level: 1

SUBLOCATIONS : Array(2)
    location : "Location Child"
    locationid : 201
    parent_location_id : 101
    level: 2

    location : "Location Child Two"
    locationid : 301
    parent_location_id : 101
    level: 2

    SUBLOCATIONS : Array(2)
        location : "Location Sub Child"
        locationid : 401
        parent_location_id: 301
        level: 3

        location : "Location Sub Child Two"
        locationid : 501
        parent_location_id: 301
        level: 3

但我无法理解如何初始化和范围深度变量以计算它在递归时的深度,并在它到达底部时重置并从顶部开始一个新的分支。

1 个答案:

答案 0 :(得分:0)

where data is returned from a service, and iteratorDepth is defined as a private variable:

Call:

this.locations = this.recursivelyTraverseLocations(data,1)

and modify the function to take the interatorDepth as a parameter

this.recursivelyTraverseLocations(locations, iteratorDepth) {

        for (let location of locations) {

            if (location.parent_location_id > 0) {
                location.level = iteratorDepth
            } else {
                location.level = 1
            }

            if (location.SUBLOCATIONS) {
                this. this.recursivelyTraverseLocations(location.SUBLOCATIONS, iteratorDepth+1);
            }
        }
        return locations;
    }

This correctly sets the level attribute on each object in the hierarchy.