i have datamodel
struct DataModel: Hashable{
var image:String
var name:String
}
var model = [DataModel(image:"1",name:"2"),DataModel(image:"3",name:"4")]
i want to Multiply the element 3 time, is there any func can do it?
var model = [DataModel(image:"1",name:"2"),DataModel(image:"3",name:"4"),DataModel(image:"1",name:"2"),DataModel(image:"3",name:"4"),DataModel(image:"1",name:"2"),DataModel(image:"3",name:"4")]
CodePudding user response:
you could try this approach to Multiply
your original model
3 times:
let model = [DataModel(image:"1",name:"2"),DataModel(image:"3",name:"4")]
var arr: [DataModel] = Array(repeating: model, count: 3).flatMap{$0}
print("----> arr: \(arr)")
CodePudding user response:
You can also do it using a simple for loop. You can easily modify inside this for loop according to your further needs.
let model = [DataModel(image:"1",name:"2"),DataModel(image:"3",name:"4")]
var newModelArray = [DataModel]()
for _ in 1...3 {
newModelArray.append(model[0])
newModelArray.append(model[1])
}