I have two JSON files and I need to get the program to log the names from the other and under each name I need to log a list of titles that belong to that name from another JSON file. They are connected with an id but on the other file the correct id is named "id" and on the other, it's "userId" (and there is a key-id also but refers to something else.
The data is from here: https://jsonplaceholder.typicode.com/posts and here https://jsonplaceholder.typicode.com/users
What I need is something like this:
name1<br>
-title1<br>
-title2<br>
-title3<br>
-title4<br>
name2<br>
-title5 <br>
-title6<br>
-title7<br>
-title8
One solution is to use a nested for-loop:
for (let user of users) {
console.log(user.name);
for (let title of titles) {
if (user.id == title.userId) {
console.log(`\t- ${title.title}`);
}
}
}
Results in something like:
Leanne Graham
- sunt aut facere repellat provident occaecati excepturi optio reprehenderit
- qui est esse
- ea molestias quasi exercitationem repellat qui ipsa sit aut
- eum et est occaecati
- nesciunt quas odio
# ...
Ervin Howell
- et ea vero quia laudantium autem
- in quibusdam tempore odit est dolorem
# ...