Search array struct in an array in the struct

search array struct swift
index of struct in array swift
swift array of structs
swift array get index of item
get index of object in array swift 4
swift array index
swift filter array of strings
swift firstindex(where)

help me find an array in the array. In this implementation, I return the entire array.

[(name: "Bell 2", arr: [arrSub(sub_txt: "Test 2, 1"),arrSub(sub_txt: "Test 2, 2")])]

I need to return:

[(name: "Bell 2", arr: [arrSub(sub_txt: "Test 2, 2")])]

// Playground

import UIKit

struct arrSub {
    let sub_txt: String

struct test {
    let name: String
    let arr: [arrSub]

    init(name: String, arr: [arrSub]) { = name
        self.arr  = arr

var testArr = [test]()

testArr.append(test(name: "Line 1", arr: [arrSub(sub_txt: "Line 1, 1"), arrSub(sub_txt: "Line 1, 2")]))
testArr.append(test(name: "Bell 2", arr: [arrSub(sub_txt: "Bell 2, 1"), arrSub(sub_txt: "Bell 2, 2")]))
testArr.append(test(name: "Bell 2", arr: [arrSub(sub_txt: "Test 2, 1"), arrSub(sub_txt: "Test 2, 2")]))

let new = testArr.filter({
    $0.arr.contains(where: { $0.sub_txt.contains("Test 2, 2") } )        


A simple filter won't help you achieve your goals, since you are trying to modify the test instances as well by filtering their arr property. You can use compactMap instead of filter to return modified test instances in case they fulfilled the filter criterion or nil if they didn't and compactMap will only keep the non-nil values.

let new = testArr.compactMap({ test->Test? in
let foundArrSub = test.arr.filter({ $0.sub_txt.contains("Test 2, 2") })
    if foundArrSub.count > 0 {
        return Test(name:, arr: foundArrSub)
    return nil

Test data:

var testArr = [Test]()

testArr.append(Test(name: "Line 1", arr: [ArrSub(sub_txt: "Line 1, 1"), ArrSub(sub_txt: "Line 1, 2")]))
testArr.append(Test(name: "Bell 2", arr: [ArrSub(sub_txt: "Bell 2, 1"), ArrSub(sub_txt: "Bell 2, 2")]))
testArr.append(Test(name: "Bell 2", arr: [ArrSub(sub_txt: "Test 2, 1"), ArrSub(sub_txt: "Test 2, 2"),ArrSub(sub_txt: "Test 2, 23")]))

[__lldb_expr_1.Test(name: "Bell 2", arr: [__lldb_expr_1.ArrSub(sub_txt: "Test 2, 2"), __lldb_expr_1.ArrSub(sub_txt: "Test 2, 23")])]

You should conform to the Swift naming convention, which is UpperCamelCase for types, so I changed test to Test and arrSub to ArrSub.

searching within an array of structs in c, You're doing a kind of binary search over the array, which is not designed to find all instances without modification. Have you considered just doing a linear  Or, in the search function, you can check whether array[i].word is a NULL; and if you find NULL, then you immediately return -1 from the function. For the second suggestion, your search function will become something like this.

Thanks, @Dávid Pásztor. I just slightly corrected

let new = testArr.compactMap { (myTest) -> test? in
if let foundArrSub = myTest.arr.first(where: { $0.sub_txt.contains("Test 2, 2") }){
    return test(name:, arr: [foundArrSub])
return nil} 

This search will find only one record always in the substructure. But I need to return all the elements that are equal to the search condition.

Get Index of Array containing Structs by searching for Struct Value , You can use index(where:) method. A quick example : let index = myArray.index(​where: { $ == "Smith" }). In a case when there would be  A structure array is a data type that groups related data using data containers called fields.Each field can contain any type of data. Access data in a field using dot notation of the form structName.fieldName.

You can use reduce also

let news =  testArr.reduce([test]()) { (result, testObj) -> [test] in

            let internalArray  = testObj.arr.filter({ (arraySub) -> Bool in
                return   arraySub.sub_txt.range(of:"Test 2, 2") != nil
            }).filter({ (arraySub) -> Bool in
                return   arraySub.sub_txt.range(of:"Test 2, 2") != nil
        if internalArray.count > 0 {
            return result + [test.init(name:, arr: internalArray)]
            return result


.contains() on an array of a struct, contains() on an array of a struct. Running the .contians() method on an array of basic types such as Int, String, etc. is fairly straight forward:. So I'm trying to assign a struct array to a field of another struct array when both have the same size and obviously trying to avoid a for loop. It looks something like this data % This is a struct array that doesnt have the field trait yet

Array of Structures in C, Here arr_car is an array of 10 elements where each element is of type struct car . We can use arr_car to store 10 structure variables of type struct car . To access  Mario and Link © Nintendo. Sonic the Hedgehog and Miles "Tails" Prower © Sega. Mega Man © Capcom.

Array as Member of Structure in C, The student structure defined above has a member name which is an array of 20 characters. Let's create another structure called student to store name, roll no and​ 

Ask a Jedi: Finding an value in an array of structures, ColdFusion comes with a structFindValue() function. This function will search through a structure, no matter how complex, and find matches