如何过滤dic数组-过滤器并追加到单独的dic数组

时间:2019-07-11 10:19:29

标签: ios swift xcode dictionary filter

我有一组dic,在我的dic中,我有一个键称为“状态”:test,“ state”:原始。我可能有多个对象。

代码:

$(document).ready(function() {
    Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI3ZTk0OWVmOS03OTNlLTQ1ZmQtYjhlZS0wNDQ1ZTA4MGM1MTQiLCJpZCI6MTI3MzcsInNjb3BlcyI6WyJhc3IiLCJnYyJdLCJpYXQiOjE1NjE3MTY2OTV9.Y5yXWhATrW7MGyDmEOGEgc3DH4gBNOo91y9NKgQ0UQc';
    var extent = Cesium.Rectangle.fromDegrees(-25.75034722222222,-0.4456061,44.204115292705865,70.20189571441897);
    Cesium.Camera.DEFAULT_VIEW_RECTANGLE = extent;   
    Cesium.Camera.DEFAULT_VIEW_FACTOR = 0.3;

    var viewer = new Cesium.Viewer('cesiumContainer', {
        scene3DOnly: true,
        selectionIndicator: true,
        baseLayerPicker: false,
        animation: false,
        timeline: false, 
        homeButton: true
    });

    var imageryLayer = viewer.imageryLayers.addImageryProvider(
        new Cesium.IonImageryProvider({ assetId: 3 })
    );

    // SPECIFY Longitude, Latitude

    var dubai = viewer.entities.add({
        name : 'Dubai',
        description : "<p><a href='http://www.capitali.org/capitali-drone-app/#/app/preview/3' target='_blank'>View live footage</a></p>",
        position : Cesium.Cartesian3.fromDegrees(55.2708, 25.2048),
        point : {
            pixelSize : 10,
            color : Cesium.Color.RED,
            outlineColor : Cesium.Color.WHITE,
            outlineWidth : 1
        },
        label : {
            text : 'Dubai',
            font : '15pt sans-serif',
            style: Cesium.LabelStyle.FILL_AND_OUTLINE,
            outlineWidth : 2,
            verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
            pixelOffset : new Cesium.Cartesian2(0, -9)
        }        
    });
});

我的方法中的小片段:

位置-> var StatusData = [[String: Any]]() var testStatusData = [[String: Any]]() var originalStatusData = [[String: Any]]()

document.data() is an [String:Any]

现在,我需要为for document in (querySnapshot! as AnyObject).documents { print("\(document.documentID) => \(document.data())") let data = document.data() self.playersStatusData.append(document.data()) } print(self.playersStatusData) 添加过滤器,并基于状态-测试原始。我需要分离对象并将其附加到各自的dic数组。

如果我的playersStatusData中有4个dic对象。在那个如果2对象有状态=“测试”。然后,仅该特定对象需要附加到playersStatusData

与状态为“原始”的另一个2个对象相同。然后,仅该特定对象需要附加到testStatusData

我使用了originalStatusData。它仅打印该键,值。但是我需要完整的对象并附加到它们各自的[String:Any]]。

对此有任何帮助,谢谢。

更新:

filter { $0.1 == "orginal" }

1 个答案:

答案 0 :(得分:0)

您可以尝试

let playersStatusData =  [
    ["test": 1, "id": 230, "total": 1, "crunch": 1, "name": "A", "Bot": "", "state": "test"],
    ["test": 2, "id": 231, "total": 1, "crunch": 1, "name": "B", "Bot": "", "state": "original"],
    ["test": 3, "id": 232, "total": 1, "crunch": 1, "name": "C", "Bot": "", "state": "test"],
    ["test": 4, "id": 233, "total": 1, "crunch": 1, "name": "D", "Bot": "", "state": "original"],
    ["test": 5, "id": 234, "total": 1, "crunch": 1, "name": "E", "Bot": "", "state": "test"],
    ["test": 6, "id": 235, "total": 1, "crunch": 1, "name": "F", "Bot": "", "state": "test"]
]


let dicts = Dictionary(grouping: playersStatusData, by: {$0["state"] as! String})

print(dicts["test"]) // O/P of all state = test in array
print(dicts["original"]) // O/P of all state = original in array

如果有任何问题,您可以询问。