Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

swift - An array and list of list properties

I have an array of the objects. Every single object has another list. How to get the whole list of properties from the list? So as you can see there is a StatioItem array. Each stationItem owns a list of sensorItem objects. I want to get a list of id properties. How to do it?

[StationItem {
    id = 114;
    stationId = 114;
    cityName = Wroc?aw;
    addressStreet = ul. Bartnicza;
    sensors = List<SensorItem> <0x6000001bb840> (
        [0] SensorItem {
            id = 642;
            stationId = 642;
            param = ParamItem {
                paramName = dwutlenek azotu;
                paramFormula = NO2;
                paramCode = NO2;
                idParam = 6;
            };
        },
        [1] SensorItem {
            id = 644;
            stationId = 644;
            param = ParamItem {
                paramName = ozon;
                paramFormula = O3;
                paramCode = O3;
                idParam = 5;
            };
        }
    );
}, StationItem {
    id = 70;
    stationId = 70;
    cityName = O?awa;
    addressStreet = ul. ?o?nierzy AK 9;
    sensors = List<SensorItem> <0x6000001b7840> (
        [0] SensorItem {
            id = 397;
            stationId = 397;
            param = ParamItem {
                paramName = dwutlenek azotu;
                paramFormula = NO2;
                paramCode = NO2;
                idParam = 6;
            };
        },
        [1] SensorItem {
            id = 400;
            stationId = 400;
            param = ParamItem {
                paramName = py? zawieszony PM10;
                paramFormula = PM10;
                paramCode = PM10;
                idParam = 3;
            };
        },
        [2] SensorItem {
            id = 20214;
            stationId = 20214;
            param = ParamItem {
                paramName = ozon;
                paramFormula = O3;
                paramCode = O3;
                idParam = 5;
            };
        }
    );
}]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What do we need to do: Iterate the array, for each station found, iterate over its sensor array. For each sensor found, keep only its id.

let ids = stations.map{ aStation in aStation.sensors.map{ $0.id } }.flatMap{ $0 }

With Sample:

struct StationItem {
    let id: Int
    let sensors: [SensorItem]
}

struct SensorItem {
    let id: Int
}

let stations: [StationItem] = [StationItem(id: 114, sensors: [SensorItem(id: 642),
                                                              SensorItem(id: 644)]),
                               StationItem(id: 70, sensors: [SensorItem(id: 397),
                                                             SensorItem(id: 400),
                                                             SensorItem(id: 2014)])]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...