根据键值创建多维数组 [英] Create multidimensional array based on value of key

查看:57
本文介绍了根据键值创建多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个arrayMyData个对象(MyData是一个struct):

I have an array of MyData objects (MyData is a struct):

[
MyData(id: 3, locale: "en", title: "p1", date: "10/15/2019"), 
MyData(id: 3, locale: "de", title: "p2", date: "11/12/2019"), 
MyData(id: 32, locale: "fr", title: "free", date: "10/11/2019"), 
MyData(id: 15, locale: "de", title: "free", date: "10/11/2019"), 
MyData(id: 19, locale: "de", title: "p1", date: "11/10/2019"),
MyData(id: 19, locale: "de", title: "p2", date: "11/10/2019"),
MyData(id: 19, locale: "de", title: "p3", date: "11/10/2019"),
]

我想基于id键将该数组分组(甚至有意创建一个新数组).

I'd like to group this array (or even purposedly create a new one) based on the id key.

结果应该是这样的:

[
[MyData(id: 3, locale: "en", title: "p1", date: "10/15/2019"), MyData(id: 3, locale: "de", title: "p2", date: "11/12/2019")], 
MyData(id: 32, locale: "fr", title: "free", date: "10/11/2019"), 
MyData(id: 15, locale: "de", title: "free", date: "10/11/2019"), 
[MyData(id: 19, locale: "de", title: "p1", date: "11/10/2019"),MyData(id: 19, locale: "de", title: "p2", date: "11/10/2019"),MyData(id: 19, locale: "de", title: "p3", date: "11/10/2019")]
]

即:具有相同ID的数组应形成一个新数组.

that is: arrays having the same id should form a new array.

当然,我可以简单地遍历第一个数组并创建第二个数组,但是我想知道Swift是否可以对其过滤器进行处理. 感谢您的帮助.

Of course, I could simply loop over the first array and create the second, but I wanted to know if there's something Swift can do with its filters. Any help is appreciated.

推荐答案

您可以肯定地在其中使用高阶函数,但不能使用 100 %来完全产生所需的结果,但是很大因为您想要的数组类型是:[Any].

You can use the higher order functions in this for sure, but not 100% to fully produce the desired result but a big chunk of it, since your desired array type is :[Any].

查看下面的代码:

var myGroup = Dictionary(grouping: arrayOne, by: { $0.id }) // group each element by id -type of: [Int:[Data]]
let resultArray = myGroup.map { $0.value } //map out the elements without the id key. -type of: [[Data]]

//Create hetro Array so we can use it later to append the results
var myHetroArray: [Any] = []
// loop each array in the result array and check if it only contains 1 element if so append that one element to the hetro array otherwise just append the whole thing.
for array in resultArray {
    if array.count ==  1 {
    myHetroArray.append(array.first!)
    } else {
        myHetroArray.append(array)
    }
}

print(myHetroArray) // produce the desired result.

输出: [
[数据(id:19,区域设置:"de",标题:"p1",日期:"11/10/2019"),数据(id:19,区域设置:"de",标题:"p2",日期: " 11/10/2019),数据(id:19,语言环境:" de,标题:" p3,日期:" 11/10/2019))],
数据(id:15,语言环境:"de",标题:"free",日期:"10/11/2019"),
数据(id:32,语言环境:"fr",标题:"free",日期:"10/11/2019"),
[数据(id:3,语言环境:"en",标题:"p1",日期:"10/15/2019"),数据(id:3,语言环境:"de",标题:"p2",日期: "11/12/2019")]
]

Output: [
[Data(id: 19, locale: "de", title: "p1", date: "11/10/2019"),Data(id: 19, locale: "de", title: "p2", date: "11/10/2019"),Data(id: 19, locale: "de", title: "p3", date: "11/10/2019")],
Data(id: 15, locale: "de", title: "free", date: "10/11/2019"),
Data(id: 32, locale: "fr", title: "free", date: "10/11/2019"),
[Data(id: 3, locale: "en", title: "p1", date: "10/15/2019"), Data(id: 3, locale: "de", title: "p2", date: "11/12/2019")]
]

这篇关于根据键值创建多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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