sort json data in javascript

1

I want to sort the data of a JSON that I obtain from an API, to group the Post by user, that is, userId = 1 to obtain all its post, the information that it gives me is the publications made by the users with id, title, its body and the userID, I would like to sort by user this is the code I'm doing.

let dataByUser = [];
 const sortByUser =data.map((data,indexs) => {
    //console.log(indexs+" "+data.body+" "+data.id +" "+data.title );
    dataByUser[data.userId] = [];
    dataByUser[data.userId][indexs] = ({body : data.body,
                                          id: data.id,
                                          title: data.title});
  return dataByUser;
 });
 console.log(sortByUser);

But that's what I get by console:

  

(100) [Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11) ), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11) ), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11) ), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11) ), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11) ), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11) ), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11) ), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11) ), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11) ), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11), Array (11)]   0   :   Array (11)   one   :   (10) [empty × 9, {...}]   two   :   (20) [empty × 19, {...}]   3   :   (30) [empty × 29, {...}]   4   :   (40) [empty × 39, {...}]   5   :   (50) [empty × 49, {...}]   6   :   (60) [empty × 59, {...}]   7   :   (70) [empty × 69, {...}]   8   :   (80) [empty × 79, {...}]   9   :   (90) [empty × 89, {...}]   10   :   (100) [empty × 99, {...}]   length   :   eleven    proto   :   Array (0)

This is the structure I get json

    
asked by Jorge E. 18.08.2018 в 04:58
source

1 answer

2

I'm not sure what you're looking for, but I'll give you a way to sort them (it's commented in the code because it's not necessary if we're going to group them together) and a way to group them so that you can access them with userId . To list all posts of userId 1: byUser[1] . To list all posts of the idUser 2: byUser[2] ... and so on.

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts');
xhr.onload = function() {
  if (xhr.status === 200) {

    var dataByUser = JSON.parse(xhr.responseText);


    /* dataByUser = dataByUser.sort((a, b) => {
    
      if (a.userId == b.userId) {
        return 0;
      }
      if (a.userId < b.userId) {
        return 1;
      }
      if (a.userId > b.userId) {
        return -1;
      }
    
    }) */

    var byUser = {};

    dataByUser.map(o => {
      if (o.userId in byUser) {
        byUser[o.userId].push(o);
      } else {
        byUser[o.userId] = [];
        byUser[o.userId].push(o);
      }
    })

    //console.log("----TODOS----")
    //console.log(byUser)
    console.log("----Todos los POST del id 1----")
    console.log(byUser[1])
    

  }
};
xhr.send();
    
answered by 18.08.2018 / 17:27
source