0

I'm having an array: var myArray = ["","dfg","erte","","asd"]

How can I get the index of those elements which doesn't have ""..?

I have this code:

for i in myArray {
                                                                    
  let index = myArray.firstIndex(where: {$0 == i})

} 

This gives me the index of all elements. But I want the index of those elements which doesn't have ""

3
  • The code you posted does not make sense. It loops through EVERY element in the array, and then on each pass, you create a local variable index, which you then discard on the next pass through the loop. What is your desired output. You say 'I want the index of those elements which [don't contain] "".' Does that mean you want to create an array containing the indexes of every array element that is not empty? Commented Jan 13, 2021 at 13:38
  • Plus, the loop will always either match the current element of the array or, if the same value appears more than once, it will return in the index of the first occurrence of any duplicate value. Commented Jan 13, 2021 at 13:44
  • which doesn't have "" You mean the string that aren't empty? Commented Jan 13, 2021 at 13:47

4 Answers 4

1

Here is an option is using filter

let range = myArray.indices.filter { !myArray[$0].isEmpty }

To access the array using range you can then do

for index in range {
    print(myArray[index])
}

Of course if the end goal is to get an array of all non-empty values then you can filter on the array directly

let notEmpty = myArray.filter { !$0.isEmpty }
Sign up to request clarification or add additional context in comments.

3 Comments

actually @Joakim Danielson, I wanted a single index like so..for i in myArray { let range = ... let id = self.myIDArray[range!] }
@user14822019 No, you want the indices of the elements that are not empty. That's what it says in your question. And if you want them one by one then loop over range
@user14822019, what do you mean you want a single index? Explain in words which index you want? The index of the first non-empty element?
1

If your goal is to create an array of the indexes of all non-empty elements in your string array, you can do that using enumerated() and compactMap:

var myArray = ["","dfg","erte","","asd"]

let indexes = myArray.enumerated().compactMap { (index, value) in
    value.isEmpty ? nil : index
}

print(indexes)

That outputs:

[1, 2, 4]

If, instead, you want the index of the first non-empty element, you could use code like this:

if let firstNonEmptyIndex = myArray.firstIndex(where: {!$0.isEmpty}) {
    print("The entry at index \(firstNonEmptyIndex) is empty")
} else {
    print("No entries in the array are empty.")
}

Comments

0

You can get index with offset value in here:

var str = ["","123","345","","789"]

for i in str.enumerated(){
    if i.element != "" {
        print(i.offset)
    }
}

1 Comment

actually my entire array is a string array..please see edited question
0

You can use compactMap it remove nil value too(if array has)

var myArrayIndex = myArray.enumerated().compactMap { (index, data) -> Int? in
    if !data.isEmpty {
        return index
    }
    return nil
}
print(myArrayIndex)

Sort way:

var myArrayIndex = myArray.enumerated().compactMap{!$0.element.isEmpty ? $0.offset : nil}
print(myArrayIndex)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.