How to get the index of an item in a 2D array?

后端 未结 4 1699
情歌与酒
情歌与酒 2020-12-12 08:14

If I have an array:

let array = [
        [\"Hamburger\", \"Nachos\", \"Lasagne\"],
        [\"Tomatoes\", \"Apples\", \"Oranges\"],
        [\"Soda\", \         


        
相关标签:
4条回答
  • 2020-12-12 08:47

    Its not elegant but simple to read

    func getIndices(arr: [[String]], word: String) -> (Int, Int)? {
      for i in 0..<arr.count {
        let subArr = arr[i]
        if let index = subArr.index(of: word) {
            return (i, index)
        }
      }
      return nil
    }
    let result = getIndices(arr: array, word: "Apples"))
    
    0 讨论(0)
  • 2020-12-12 08:48
    let array = [
            ["Hamburger", "Nachos", "Lasagne"],
            ["Tomatoes", "Apples", "Oranges"],
            ["Soda", "Juice", "Water"]
    ]
    
    for arr in array {
    
      let answer = arr.indexOf("Apples")
      if answer {
        break
      }
    print(answer)
    }
    
    0 讨论(0)
  • 2020-12-12 08:49

    Another option as an extension. The function tupleIndex(of:) returns a tuple of (Int, Int)?.

    let array = [
        ["Hamburger", "Nachos", "Lasagne"],
        ["Tomatoes", "Apples", "Oranges"],
        ["Soda", "Juice", "Water"]
    ]
    
    extension Collection where
        Element: Collection,
        Element.Element: Equatable,
        Element.Index == Int {
    
        func tupleIndex(of elementToFind: Element.Element) -> (Int, Int)? {
            for (firstIndex, element) in self.enumerated() {
                if let secondIndex = element.index(of: elementToFind) {
                    return (firstIndex, secondIndex)
                }
            }
            return nil
        }
    }
    

    You can use it like this:

    print(array.tupleIndex(of: "Apples")) //prints Optional((1, 1))
    
    0 讨论(0)
  • 2020-12-12 08:53

    You can use firstIndex(where:) and find the subindex of it using firstIndex(of:):

    let array = [
        ["Hamburger", "Nachos", "Lasagne"],
        ["Tomatoes", "Apples", "Oranges"],
        ["Soda", "Juice", "Water"]
    ]
    

    let query = "Apples"
    if let index = array.firstIndex(where: {$0.contains(query)}),
        let subIndex = array[index].firstIndex(of: query) {
        print(array[index][subIndex])  // Apples
    
    }
    

    As an Extension:

    extension Collection where Element: Collection, Element.Element: Equatable {
        func indexAndSubIndex(of element: Element.Element) -> (index: Index, subIndex: Element.Index)? {
            if let index = firstIndex(where: {$0.contains(element)}),
                let subIndex = self[index].firstIndex(of: element) {
                return (index,subIndex)
            }
            return nil
        }
    }
    

    usage:

    let array = [
        ["Hamburger", "Nachos", "Lasagne"],
        ["Tomatoes", "Apples", "Oranges"],
        ["Soda", "Juice", "Water"]
    ]
    let query = "Soda"
    if let indexes = array.indexAndSubIndex(of: query) {
        print(indexes)   // "(index: 2, subIndex: 0)\n"
    }
    

    This would work also to find the index of a character from an array of strings:

    let array = ["Hamburger", "Nachos", "Lasagne"]
    let query: Character = "h"
    if let indices = array.indexAndSubIndex(of: query) {
        print(indices)   // "(index: 1, subIndex: Swift.String.Index(_rawBits: 196865))\n"
        array[indices.index][indices.subIndex]  // "h"
    }
    
    0 讨论(0)
提交回复
热议问题