Issue Description
There are two APIs I am working with. The first one, let's call it API #1, provides JSON data related to forum posts as shown below:
[{
"userId": 1,
"id": 10,
"title": "Tt1",
"body": "qBb2"
},
{
"userId": 2,
"id": 11,
"title": "TT4",
"body": "BBB5"
}]
The second API, referred to as API #2, contains personal information such as names and user IDs.
[
{
"id": 1,
"name": "Lera",
"username": "Lera2",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ebae86abc5888486">[email protected]</a>",
"address": {
"street": "GGa",
"suite": "Ap3. 333",
"city": "Gwee",
"zipcode": "2222-3333",
"geo": {
"lat": "-11213",
"lng": "312424"
}]
I aim to extract unique names from API #2 and place each name within a div labeled "User". Subsequently, I plan to display the posts from API #1 under the corresponding User div based on a matching field ("id").
Progress Made So Far
fetch('API-#2')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
document.getElementById("testDiv").innerHTML = JSON.stringify(myJson);
});
The current output displays raw data from API #2 without any processing.
Challenges Faced
- I need to utilize methods like map, reduce, or similar to iterate through names in API #2 and create respective User divs.
- It is necessary to loop through posts in API #1 and assign each post under the appropriate User div by matching their 'id' fields.
UPDATE
fetch('API-#2')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
myJson.filter(function (item, index, self) { return self.indexOf(item) === index; }).forEach(function (user) {
document.getElementById("testDiv2").innerHTML += "<div>" + user.name + "</div>";
});
});
The above code snippet successfully lists out the unique names fetched:
Name 1
Name 2
Name 3
My next step involves associating the posts with the corresponding names using their IDs for cross-referencing.