Home > front end >  How to sort a nested query result
How to sort a nested query result

Time:09-26

I have the following output:

    {
      "data": {
        "title": "Movie 4",
        "data": [
          {
            "userId": 2,
            "profile": { "profileImage": "image" },
            "round": 1,
          },
         {
            "userId": 4,
            "profile": { "profileImage": "image" },
            "round":6,
          },
          {
            "userId": 10,
            "profile": { "profileImage": "image" },
            "round": 4,
          },
        ]
      }
    }

The output im expecting is the following:

{
  "data": {
    "title": "Movie 4",
    "data": [
      {
        "userId": 2,
        "profile": { "profileImage": "image" },
        "round": 1,
      },
      {
        "userId": 10,
        "profile": { "profileImage": "image" },
        "round": 4,
      },
     {
        "userId": 4,
        "profile": { "profileImage": "image" },
        "round":6,
      },
    ]
  }
}

I want to sort my output by my nested value round.

This is where im collecting all the data, but i dont know how to sort it:

let combinedResult = { 
  title: combinations["data"]["title"],
  data: galleryData.map((item, i) => {
     let combination = combinations["data"]["eventdata"].find(c => c.partner === item.userId);
    return { ...item, round: combination.round, roundStart: combination.roundStart, roundEnd: combination.roundEnd} 
  })
};

I already tried to sort before the return with this but it didnt work:

const sorted = Object.entries(resultAfter)
.sort(([keyA], [keyB]) => keyA.localeCompare(keyB));

CodePudding user response:

Another option is to use sort() method

let json = {
      "data": {
        "title": "Movie 4",
        "data": [
          {
            "userId": 2,
            "profile": { "profileImage": "image" },
            "round": 1,
          },
         {
            "userId": 4,
            "profile": { "profileImage": "image" },
            "round":6,
          },
          {
            "userId": 10,
            "profile": { "profileImage": "image" },
            "round": 4,
          },
        ]
      }
    }
    
json.data.data.sort((a,b) => a.round - b.round)
console.log(json)

CodePudding user response:

You can use sortBy from lodash.

import { sortBy } from "lodash";

const sortedData = sortBy(movie.data.data, "round");

Try it on code sandbox.

CodePudding user response:

You can simply sort the array inside the object data. In this way you are editing the same array inside the object data, sort() is a destructive method that doesn't create a copy but modify the original array

let yourData = {
      "data": {
        "title": "Movie 4",
        "data": [
          {
            "userId": 2,
            "profile": { "profileImage": "image" },
            "round": 1,
          },
         {
            "userId": 4,
            "profile": { "profileImage": "image" },
            "round":6,
          },
          {
            "userId": 10,
            "profile": { "profileImage": "image" },
            "round": 4,
          },
        ]
      }
    }
    
    let arrayData = yourData.data.data;
    let result = arrayData.sort(compareFunction);
    console.log(yourData);
    
    function compareFunction(keyA,keyB){
      return keyA.round-keyB.round;
    }

CodePudding user response:

You can build a new object by spreading out the properties of the current one, and sorting the nested array.

const obj={data:{title:"Movie 4",data:[{userId:2,profile:{profileImage:"image"},round:1},{userId:4,profile:{profileImage:"image"},round:6},{userId:10,profile:{profileImage:"image"},round:4}]}};

const out = {
  data: {
    ...obj.data,
    data: obj.data.data.sort((a, b) => {
      a.round - b.round;
    })
  }
};

console.log(out);

  • Related