Get data from JSON, only if it is in array

Hi, I am trying to create a function that when passed a URL containing a JSON file and a list of names, it returns me some structure.

I have created (with help) the function that returns all the values of the JSON without problem, but I am having problems filtering them and only returning those that are in the array.

Code Block
let urlString = "https://jsonplaceholder.typicode.com/users"
let arrayInput = ["Leanne Graham",
"Nicholas Runolfsdottir V"]

This is what I use for the function that returns all data:

Code Block
struct WelcomeElement: Codable {
let name: String?
let username: String?
let email: String?
}
typealias Datos = [WelcomeElement]
class UserItems {
var purchases = [WelcomeElement]()
func loadData(urlString: String, completion: @escaping (Datos) -> Void) {
let url = URL(string: urlString)!
URLSession.shared.dataTask(with: url) { data, _, error in
if let error = error as NSError?, error.domain == NSURLErrorDomain, error.code == NSURLErrorNotConnectedToInternet {
print("Not connected")
}
do {
let items = try JSONDecoder().decode([WelcomeElement].self, from: data!)
self.purchases = items
completion(items)
} catch {
print(error)
}
}.resume()
}
}


And this is the function that I have created to return certain values but I get an error and I don't know if it is well handled.

Code Block
class UserItems {
var purchases = [WelcomeElement]()
var purchases_new = [WelcomeElement]()
func loadData(urlString: String, arrayInput: Array<String>, completion: @escaping (Datos) -> Void) {
let url = URL(string: urlString)!
URLSession.shared.dataTask(with: url) { data, _, error in
if let error = error as NSError?, error.domain == NSURLErrorDomain, error.code == NSURLErrorNotConnectedToInternet {
print("Not connected")
}
do {
let items = try JSONDecoder().decode([WelcomeElement].self, from: data!)
for position in 0...items.count-1 {
for a in arrayInput {
if a == items[position].name {
print(items[position])
self.purchases_new = items[position] // Cannot assign value of type 'WelcomeElement' to type '[WelcomeElement]'
}
}
}
self.purchases_new = items
completion(items)
} catch {
print(error)
}
}.resume()
}
}

Surely there is a more efficient way to find the value of the array in the json but that was the only thing that occurred to me.

This function, I use it to create a table with the structure.

Any help is welcome, thank you!
Answered by Claude31 in 674827022
You need to append the element to the array.

Replace
Code Block
self.purchases_new = items[position] // Cannot assign value of type 'WelcomeElement' to type '[WelcomeElement]'
by
Code Block
self.purchases_new.append(items[position])

At line 9, if not connected, you should exit:
Code Block
print("Not connected")
return

After line 17, you should break: no need to continue searching (if I understood well your code).

line 21:
Code Block
self.purchases_new = items

That will crash the array you have just built. Why do you do this ?

You can also simplify greatly your code by replacing:
Code Block
for position in 0...items.count-1 {
for a in arrayInput {
if a == items[position].name {
purchases_new.append(items[position])
break
}
}
}

with
Code Block
purchases_new = items.filter() { arrayInput.contains($0.name)}

Accepted Answer
You need to append the element to the array.

Replace
Code Block
self.purchases_new = items[position] // Cannot assign value of type 'WelcomeElement' to type '[WelcomeElement]'
by
Code Block
self.purchases_new.append(items[position])

At line 9, if not connected, you should exit:
Code Block
print("Not connected")
return

After line 17, you should break: no need to continue searching (if I understood well your code).

line 21:
Code Block
self.purchases_new = items

That will crash the array you have just built. Why do you do this ?

You can also simplify greatly your code by replacing:
Code Block
for position in 0...items.count-1 {
for a in arrayInput {
if a == items[position].name {
purchases_new.append(items[position])
break
}
}
}

with
Code Block
purchases_new = items.filter() { arrayInput.contains($0.name)}

Get data from JSON, only if it is in array
 
 
Q