用条件合并数组 [英] Merge arrays with condition

查看:115
本文介绍了用条件合并数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



首先我的结构在数组中:

  struct Item {
var id:Int
var name:String
var value:Int
}

这两个数组的第二个元素:

  let fisrt = Item(id:1,name:Oleg,value:3)
let second = Item(id:2,name:Olexander,value: 5)

let fisrtInSecond = Item(id:1,name:Bogdan,value:6)
let secondInSecond = Item(id:2,name:Max,value: 9)

阵列:

  var fisrtArray = [fisrt,second] 
let secondArray = [fisrtInSecond,secondInSecond]

我很想使用集合的 zip map 函数来存档结果。结果是由 id 更新 fisrtArray 元素名称
$ b

示例: fisrtArray = [Item(id:1,name:Bogdan,value:3),Item(id:2,name :Max,value:5)]



我知道如何通过简单循环来实现。但我在寻找更高级的函数式编程用法Swift。



我的实验:

  fisrtArray = zip(fisrtArray,secondArray).map()

问题我不知道如何在 map 函数中写入条件。条件应该是:

  if($ 0.id == $ 1.id){
$ 0.name = $ 1。名称
}

从讨论的注释中可以突出显示 zip 不适用于我的情况,因为我们应该遍历所有数组,以查找是否有类似的id,它们的顺序不同。

解决方案

下面的代码通过2个数组中的元素的顺序独立工作

  firstArray = firstArray.map {(item) - > Item 
guard
let index = secondArray.index(其中:{$ 0.id == item.id})
else {返回项目}
var item = item
item.name = secondArray [index] .name
return item
}



< blockquote>

[Item(id:1,name:Bogdan,value:3),Item(id:2,name:Max,value:5)] \\\
/ p>



更新



以下版本使用 first(其中:方法由 Martin R建议

  firstArray = firstArray.map {item  - > Item in 
guard let secondElm = secondArray.first(其中:{$ 0.id == item.id})else { return item}
var item = item
item.name = secondElm.name
return item
}


I would like to merge two arrays with specific condition and update objects that they are containing.

First my struct that is in arrays:

struct Item {
    var id:Int
    var name:String
    var value:Int
}

Second elements for the two arrays:

let fisrt = Item(id: 1, name: "Oleg", value: 3)
let second = Item(id: 2, name: "Olexander", value:5)

let fisrtInSecond = Item(id: 1, name: "Bogdan", value: 6)
let secondInSecond = Item(id: 2, name: "Max", value: 9)

Arrays:

 var fisrtArray = [fisrt, second]
 let secondArray = [fisrtInSecond, secondInSecond]

I woudl like to use zip and map functions of the collection to achive result. Result is that fisrtArray elements names are updated by id.

Example: fisrtArray = [Item(id: 1, name: "Bogdan", value:3), Item(id: 2, name: "Max", value:5)]

I know how to do this via simple loops. But i am looking for more advanced usage of the functional programing is Swift.

My experiment:

fisrtArray = zip(fisrtArray, secondArray).map()

The main problem i do not know how to write condition in the map function. Condition should be:

if ($0.id == $1.id) {
   $0.name = $1.name
}

From the comment discussing it is possible to highlight that zip is not suitable in my case because we should iterate over all array to find if we have similar id's that are not in the same order.

解决方案

The following code does work independently by the order of the elements inside the 2 arrays

firstArray = firstArray.map { (item) -> Item in
    guard
        let index = secondArray.index(where: { $0.id == item.id })
        else { return item }
    var item = item
    item.name = secondArray[index].name
    return item
}

"[Item(id: 1, name: "Bogdan", value: 3), Item(id: 2, name: "Max", value: 5)]\n"

Update

The following version uses the first(where: method as suggested by Martin R.

firstArray = firstArray.map { item -> Item in
    guard let secondElm = secondArray.first(where: { $0.id == item.id }) else { return item }
    var item = item
    item.name = secondElm.name
    return item
}

这篇关于用条件合并数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆