如何将 2 个字典合并到一个数组中?

How can I merge 2 dictionaries into one array?

我的 JSON 数据如下图所示。现在我想将 Shop Type 和 Promotion 的 value 合并为一个,用作集合视图数据。我该怎么做?

我只是像这样过滤来自服务器的响应数据:

        var dataBanDau: [SDFilterModel] = []
        var quickData: [SDFilterModel] = []

        let filters: [SDFilterModel] = data

        self.filterEntries = filters

        //let nsarray = NSArray(array: self.filterEntries! , copyItems: true)
//            self.filterEntriesStoreConstant = nsarray as! Array

        self.dataBanDau = filters

        for i in 0..<self.dataBanDau.count {
            if self.dataBanDau[i].search_key.count == 0 {
                self.quickData.append(self.dataBanDau[i])
            }
        }

        self.quickData = self.quickData.filter {
            [=10=].type != "range"
        }

        DispatchQueue.main.async {
            //Note: Reload TableView
            self.quickFilterCollection.reloadData()
            completed(true)
        }
    }

class SDFilterModel:

class SDFilterModel: DSBaseModel {
    var name = String()
    var type = String()
    var is_expanded = Int()
    var search_key = String()
    var filterEntries : [SDFilterModel]?

    override func copy(with zone: NSZone? = nil) -> Any {
        // This is the reason why `init(_ model: GameModel)`
        // must be required, because `GameModel` is not `final`.
        let copy = SDFilterModel(dict: self.dictionary)

        if let arrAttribute = NSArray(array: self.value , copyItems: true) as? [AttributeValueModel] {
            copy.value = arrAttribute
        }

        return copy
    }

    override init(dict: Dictionary<String, Any>) {
        super.init(dict: dict);
        value = self.valueParse()

        name = dict.getString(forKey: "name")
        type = dict.getString(forKey: "type")
        search_key = dict.getString(forKey: "search_key")
        is_expanded = dict.getInt(forKey: "is_expanded")!
    }


    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    var value: [AttributeValueModel] = [];

    func valueParse()-> [AttributeValueModel] {
        guard let childs  = (self.dictionary["value"]) as? [Dictionary<String, AnyObject>]
            else { return [] }

        var output: [AttributeValueModel] = [];

        for  aDict in childs {
            let item = AttributeValueModel(dict:aDict);

//            if type == .Range  && item.option_id == "0" {
//                item.setRangeOptionID(aValue: item.option_name!)
//            }
//
            output.append(item);
        }

        return output;
    }

像这样创建模型

struct Option {
    let name: String
    let searchKey: String
    let id: String
}

struct Model {
    let type: String
    let name: String
    let isExpanded: Bool
    let value: [Option]
}

您应该获取 options 数组值并连接所有数组

let models:[Model] = //...
let collectionViewArray = models.map { [=11=].value }.reduce([Option](), +)

使用for循环

var collectionViewArray = [Option]()
for model  in models {
    collectionViewArray.append(contentsOf: model.value)
}

假设您有 let myArray = [1,2,3,4,5,6,7,8]

现在您想对数组中的每个元素求平方,

使用 for 循环你可以这样做

for item in myArray {
    print(item * item) 
}

现在假设 item = [=15=]

对于 map 你只需要

myArray.map({ [=11=] * [=11=] })

两者都会给出相同的输出。


map : 用于对数组的每个元素执行相同的操作。

flatmap : array的数组扁平化

let myArr = [[1,2,3],[4,5,6,7]]

而你想要 o/p 作为 [1,2,3,4,5,6,7]

所以可以用myArr.flatMap({[=17=]})

得到上面的输出

现在回到你的问题。

let reqArray = myModel.data.map({ [=13=].value }).flatMap({ [=13=] })

首先,map 为您提供了键 value 的数组数组,但您需要一个数组,因此您需要使用 flatmap.


你可以参考:https://medium.com/@Dougly/higher-order-functions-in-swift-sorted-map-filter-reduce-dff60b5b6adf