Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftyJson - Remove empty elements from array

I have an array from JSON response. In this array from 0-20 positions. In every position can be an image or video. From the cycle I get the urls with images and videos. Sometimes I had 8 images and 2 videos, But had response - 8 urls of images, 2 urls of video, 6 empty urls of video. How I can remove empty video urls?

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.............    {…}

My Code

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 ....

My response

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”,
"",
"",
"",
"",
"",
""]
like image 345
Woop Avatar asked Feb 26 '18 22:02

Woop


1 Answers

Try this:

    private func prepare_array()
    {
        var array = ["", "not empty 1", "", "", "", "not empty 2", "not empty 3"]
        array = array.filter({ $0 != ""})
        print(array) 
        //["not empty 1", "not empty 2", "not empty 3"]
    }
like image 151
Angel Avatar answered Nov 07 '22 08:11

Angel