简体   繁体   English

如何根据另一个数组元素更新数组的所有对象

[英]How to update all objects of array based on another array element

Suppose I have two array of same object type, Now I want to update all the elements of first array based on some key of another array.假设我有两个相同对象类型的数组,现在我想根据另一个数组的某个键更新第一个数组的所有元素。 I want to update isSelected to true if myArray id ,matches with testArray id . I want to update isSelected to true if myArray id ,matches with testArray id I can ietrate through myArray using for loop and check every id which is found in the testArray and based on that index isSelected value can be updated.我可以使用 for 循环遍历 myArray 并检查在 testArray 中找到的每个 id 并基于该索引 isSelected 值可以更新。 But I am wondering if I can use higher order function like contains or filter for this use case.但我想知道我是否可以在这个用例中使用更高阶的函数,比如containsfilter

class MyObject
{
    var id: Int
    var isSelected:Bool?
    var value: String
}

var myArray = [{id: 1, isSelected: nil, value: abcd}, {id: 2, 
isSelected: nil, value: xyz}, {id: 3, isSelected: nil, value: hghghg} ]

var testArray = [{id: 1, isSelected: nil, value: abcd}, {id: 2, 
isSelected: nil, value: xyz}]

let resultArray = [{id: 1, isSelected: true, value: abcd}, {id: 2, 
isSelected: true, value: xyz}, {id: 3, isSelected: nil, value: hghghg}] 

You can try你可以试试

let testArrIds = testArray.map { $0.id }
myArray.forEach { $0.isSelected =  testArrIds.contains($0.id) ? true : $0.isSelected  }

It can be 1 line but i don't want to map the ids every loop of forEach as it would be expensive它可以是 1 行,但我不想在 forEach 的每个循环中映射 id,因为它会很昂贵

myArray.forEach { $0.isSelected =  testArray.map { $0.id }.contains($0.id) ? true : $0.isSelected  }

I did some changes to your class and arrays to make the code work我对您的类和数组进行了一些更改以使代码正常工作

class MyObject: CustomStringConvertible {
    var id: Int
    var isSelected:Bool?
    var value: String

    var description: String {
        return "\(id) \(value) \(isSelected ?? false)"
    }

    init(id: Int, value: String) {
        self.id = id
        self.value = value
    }
}

var myArray = [MyObject(id: 1, value: "abcd"), MyObject(id: 2, value: "xyz"), MyObject(id: 3, value: "hghghg") ]

var testArray = [MyObject(id: 1, value: "abcd"), MyObject(id: 2, value: "xyz")]

testArray.forEach( { myObject in
    if let first = myArray.first(where: { $0.id == myObject.id }) {
        first.isSelected = true
    }
})

print(myArray)

[1 abcd true, 2 xyz true, 3 hghghg false] [1 abcd 真,2 xyz 真,3 hghghg 假]

the above solution works well for class objects but not for structs, we can do it the following way for structs if someone is specifically looking for that like me.上述解决方案适用于类对象,但不适用于结构体,如果有人像我一样专门寻找结构体,我们可以通过以下方式对结构体进行处理。

struct Tag {
    let title: String
    var isSelected: Bool = false
}

let fetched = array of tags fetched from server
let current = array of currently selected tags 

Now we want to update the tags from server to get selected using currently selected tags现在我们要更新来自服务器的标签以使用当前选择的标签被选中

let updatedTags = fetched.map { fetchedTag in
    var updatedTag = fetchedTag
    if current.map({ $0.title }).contains(updatedTag.title) {
        updatedTag.isSelected = true
    }
    return updatedTag
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个数组中的对象从数组中删除特定对象 - Remove particular objects from an array based on objects from another array 如何使用KVO根据基础数组元素更改来更新tableViewCells? - How to use KVO to update tableViewCells based on underlying array element changes? 比较一个nsmutable数组的所有对象与另一个nsmutable数组的所有对象,并将新对象放入另一个nsarray - compare all the objects of one nsmutable array with all the objects of another nsmutable array and get new objects into another nsarray 根据与特定属性对应的另一个数组中的值对对象数组进行排序 - Sort array of objects based on values in another array that correspond to specific properties 如何检查数组的元素是否在另一个数组中? - How to check if element of an array is in another array? 如何基于另一个子数组过滤数组的数组 - How to filter array of array based on another subarray 如何从另一个阵列更新一个阵列 - How to update one array from Another Array 如何在不枚举的情况下更新swift3中字典数组中所有对象的键值? - How to update value of a key of all objects from array of dictionary in swift3 without enumeration? 如何基于其他数组的对象从数组中删除对象 - How to remove objects from array based on other array's object 如何将一个对象数组的属性分配给另一个字符串数组? - How to assign the property of one array of objects to another array of strings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM