How can I filter a 2d array in Swift?

Abdul Rehman

I'm trying to filter data from 2d array according to the string/text which is entered by user like if we write the name or phone number of a person it contact detail is searched and shown.

In 2D array [["Name","Phone number"]]. if number or name which is entered by the user is matched with any value then that will store in an other array.

var data : [[String]] = [["AB","+923476760226"],["Umair","+923366111830"],.......]
Moriya

The way to filter a 2D array is to filter twice, once per level.

var data = [["AB","+923476760226"],["Umair","+923366111830"]]
let searchString = "+9234"

var result = data.filter { (dataArray:[String]) -> Bool in
    return dataArray.filter({ (string) -> Bool in
        return string.containsString(searchString)
    }).count > 0
}

This code just filters out all the items that has at least one item that contains the searchString.

However, I'd like to say that you really should keep the name and number in a separate class or struct and build some mach function so that you don't have to filter twice

struct Contact{
    let name: String
    let number: String

    init(name: String, number:String) {
        self.name = name
        self.number = number
    }

    func match(string:String) -> Bool {
        return name.containsString(string) || number.containsString(string)
    }
}


let properData = [Contact(name: "AB", number: "+923476760226"), Contact(name: "Umair", number: "+923366111830")]

let properResult = properData.filter { (contact) -> Bool in
    return contact.match(searchString)
}

This will make your code a bit cleaner in the long run and it's a good idea to work with classes and struct like this in general

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I fully reallocate a 2D array in C?

分類Dev

Why can't I make a copy of this 2d array in JS? How can I make a copy?

分類Dev

Why can't I make a copy of this 2d array in JS? How can I make a copy?

分類Dev

How can I get array of UIStoryboard in Swift?

分類Dev

C - How can I print random word inside game board (2d array)?

分類Dev

How can i pass a 2d array to JTable in Netbeans efficiently?

分類Dev

How can I check the mouse's position over a 2D array of images for a game?

分類Dev

How can I use Array.filter() to return items in blocks of 2?

分類Dev

How can I filter a JavaScript array by element first letter?

分類Dev

I can't assign a value to a 2d array

分類Dev

How can I pass a Swift array as an argument to a thread?

分類Dev

How can I implement a zoom function in a 2D canvas?

分類Dev

How can I filter rows if one array contains all values from another array using BigQuery?

分類Dev

How can I use swift 2 and swift 3 at the same time?

分類Dev

SWIFT how to sort 2D array in 2nd dimension without having to transpose

分類Dev

How can I get a specific value from my JSON object without using Array.filter()?

分類Dev

How can I filter my array results when the user types in a search bar similarly to how it'd be done using a Model's DB table?

分類Dev

How to I check if every row in a 2D array has at least 2 elements?

分類Dev

How can I make a firebase filter with a variable?

分類Dev

how can I make a custom overlay filter?

分類Dev

How can i get request in Filter

分類Dev

How can I use both filter and filter_complex with ffmpeg?

分類Dev

How can I destructure this array?

分類Dev

How do I declare a 2d array in C++ using new?

分類Dev

How do I check the neighbors of the cells around me in a 2D array (C++)?

分類Dev

How do I read from/write to a binary file in 2D Array form?

分類Dev

How do I get the function to print out a 2D array into two files in java?

分類Dev

how can i call return array from Swift class to swiftui view file

分類Dev

How can I change the time between sprites in 2D animations on Unity?

Related 関連記事

  1. 1

    How can I fully reallocate a 2D array in C?

  2. 2

    Why can't I make a copy of this 2d array in JS? How can I make a copy?

  3. 3

    Why can't I make a copy of this 2d array in JS? How can I make a copy?

  4. 4

    How can I get array of UIStoryboard in Swift?

  5. 5

    C - How can I print random word inside game board (2d array)?

  6. 6

    How can i pass a 2d array to JTable in Netbeans efficiently?

  7. 7

    How can I check the mouse's position over a 2D array of images for a game?

  8. 8

    How can I use Array.filter() to return items in blocks of 2?

  9. 9

    How can I filter a JavaScript array by element first letter?

  10. 10

    I can't assign a value to a 2d array

  11. 11

    How can I pass a Swift array as an argument to a thread?

  12. 12

    How can I implement a zoom function in a 2D canvas?

  13. 13

    How can I filter rows if one array contains all values from another array using BigQuery?

  14. 14

    How can I use swift 2 and swift 3 at the same time?

  15. 15

    SWIFT how to sort 2D array in 2nd dimension without having to transpose

  16. 16

    How can I get a specific value from my JSON object without using Array.filter()?

  17. 17

    How can I filter my array results when the user types in a search bar similarly to how it'd be done using a Model's DB table?

  18. 18

    How to I check if every row in a 2D array has at least 2 elements?

  19. 19

    How can I make a firebase filter with a variable?

  20. 20

    how can I make a custom overlay filter?

  21. 21

    How can i get request in Filter

  22. 22

    How can I use both filter and filter_complex with ffmpeg?

  23. 23

    How can I destructure this array?

  24. 24

    How do I declare a 2d array in C++ using new?

  25. 25

    How do I check the neighbors of the cells around me in a 2D array (C++)?

  26. 26

    How do I read from/write to a binary file in 2D Array form?

  27. 27

    How do I get the function to print out a 2D array into two files in java?

  28. 28

    how can i call return array from Swift class to swiftui view file

  29. 29

    How can I change the time between sprites in 2D animations on Unity?

ホットタグ

アーカイブ