[英]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.但我想知道我是否可以在这个用例中使用更高阶的函数,比如
contains
或filter
。
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.