How to add new item to multiple dimension array?
struct Cat: Codable { var childrens: [Cat]? var name: String? var colour: String? }
This is my current array
var currentShop = [
[
"children": [
[
"children": [],
"name": "Tom",
"colour": "Brown"
],
[
"children": [],
"name": "Anggie",
"colour": "White"
],
[
"children": [
[
"children": [],
"name": "Lily",
"colour": "White"
]
],
"name": "Snowy",
"colour": "White"
],
],
"name": "Sandy",
"colour": "Brown"
],
[
"children": [],
"name": "Mike",
"colour": "Black"
],
[
"children": [],
"name": "Tommy",
"colour": "White"
],
"name": "Blackie",
"colour": "Black"
]
New Array
var newChildren = [
[
"children": [],
"name": "Pus",
"colour": "White"
],
[
"children": [],
"name": "Boots",
"colour": "White"
]
]
Adding in the new children for Cat name under Snowy.
How do I able to update the cats in the currentShop array?
My current code to find the parent
func updateCatShop(parentName:String, Cats: [Cat], newCats:[Cat] ) -> [Cat] {
var catList = Cats for var cat in Cats {
if(cat.name == parentName) {
var updateCats = cat.childrens! + newCats cat.childrens = updateCats
} else {
for (_, childCat) in cat.childrens!.enumerated(){
updateCatShop(parentName: parentName, Cats: [childCat], newCats: newCats)
}
}
}
return catList
}
var currentShop = updateCatShop(parentName:"Snowy", Cats:currentShop, newCats: newChildren)
What would be the best way to update a multiple dimension object?
childrenis plurals, don't name itchildrens), Mike and Tommy (with no children). But Blackie appears to be incorrect. There is no Cat object associated with them.