0

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
nima
  • 7,796
  • 12
  • 36
  • 53
Karhur
  • 3
  • 1
  • Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Oct 03 '21 at 17:33
  • Please, read this [https://stackoverflow.com/help/how-to-ask] – ABDULLOKH MUKHAMMADJONOV Oct 03 '21 at 17:46

1 Answers1

0

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
    # ...

Sparrow1029
  • 592
  • 5
  • 13