SwiftyJson - 从数组中删除空元素

SwiftyJson - Remove empty elements from array

我有一个来自 JSON 响应的数组。在这个数组中从 0-20 个位置。每个位置都可以是图像或视频。从循环中我得到了带有图像和视频的 url。有时我有 8 张图片和 2 个视频,但有响应 - 8 个图片网址、2 个视频网址、6 个空视频网址。如何删除空视频网址?

edge.........   
  edges 
   0    
   node 
   __typename   "........"
   display_resources    
   0    {…}
   1    {…}
   2    {…}
   is_video     false
   edge.........{…}

   1    
   node 
 __typename "........"
   display_resources    
   0    {…}
   1    {…}
   2    {…}
   video_url      "https://...................fods1-1.fna.fbcdn.net/vp/64633261764d268afd5d6a654d845590/5A94EC04/t50.2886-16/27988152_1901827653461655_8604903370520797664_n.mp4"
   is_video  true
   edge.............    {…}

我的代码

var json = JSON(response.result.value ?? [])
...
if self.json["..........."].stringValue == ......... {
    if let slideShow = self.json["........"].array {
        for allMedia in slideShow {
            self............append(allMedia["node"][2]["src"].stringValue)
        }
        print("One of All Media \(self.graphSidecarImage[0])")
    }

    if let allVideo = self.json["........"][].array {
        for videos in allVideo {
            self.graphSidecarVideo.append(videos["node"]["video_url"].stringValue)
        }
        print("Video = \(self........)")
    }

} else ....

我的回复

One of All Media = 
https://...........fna.fbcdn.net/vp/873e7dd29985cbb359a6bed475b5eb4a/5B4BE8A4/t51.2885-15/e35/27877630_529424757457590_5205275138961965056_n.jpg


Video = ["",
“https://............fods1-1.fna.fbcdn.net./vp/4500643b92f7564ce0b1d03266bad3b8/5A963D84/t50.2886-16/27988152_1901827653461655_8604903370520797664_n.mp4”,
"",
“https://..............fods1-1.fna.fbcdn.net/vp/751ff5e4e64b0bb8e9f5766835bf64b8/5A96707B/t50.2886-16/28257257_152261638823479_2625135617605674147_n.mp4”,
"",
"",
"",
"",
"",
""]

试试这个:

    private func prepare_array()
    {
        var array = ["", "not empty 1", "", "", "", "not empty 2", "not empty 3"]
        array = array.filter({ [=10=] != ""})
        print(array) 
        //["not empty 1", "not empty 2", "not empty 3"]
    }