2

I have an array of objects :

   myData = [{
       name: "",
       lastName:"",
       moreData: [{
           left: 5,
           data: '',

        },
         {
           left: 3,
           data: '',

        }
     ]



   },
   {
       name: "",
       lastName:"",
       moreData: [{
           left: 8,
           data: '',

        },
         {
           left: 4,
           data: '',

        }
     ]
  }
  ],

I need the to sort the outer objects (main array) based on left:, descending order, so I would have such outcome:

   myData = [{
       name: "",
       lastName: "",
       moreData: [{
            left: 8,
            data: '',
     }]

  },
  {
       name: "",
       lastName: "",
       moreData: [{
            left: 5,
            data: '',
     }]

  },
{
       name: "",
       lastName: "",
       moreData: [{
            left: 4,
            data: '',
     }]

  },

{
       name: "",
       lastName: "",
       moreData: [{
            left: 3,
            data: '',
     }]

  }

  ]

or is there a way to have only moreData sorted regardless of what object it belongs to, and save that and then be able to identify whose user the moreData belongs to ?

I need to sort the array based on a column that has multiple objects inside the its array. so the outer object will be repeated Using JS, can even use Lodash if necessary. Any guidance?

JennyJ
  • 53
  • 1
  • 8
  • 2
    Welcome to Stackoverflow, your question has already be asked multiple times. Before posting a question I recommend using the search function. Something like https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript should answer your question – Doomenik Mar 19 '18 at 10:35
  • Possible duplicate of [Sort array of objects by string property value in JavaScript](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) – Doomenik Mar 19 '18 at 10:37
  • @Doomenik My question differs as it needs to sort the array of objects based on one attribute that is also an array of objects. So those objects have a field which I need to take so that I sort accordingly. My expected results makes this more clear, as you can see the object can be repeated if because of the order I need. – JennyJ Mar 19 '18 at 10:45
  • The linked also explains how to build a custom sort either you go for that or simple split your array before the sort. I will add a quick example. – Doomenik Mar 19 '18 at 10:54

1 Answers1

1

Based on: Sort array

So here is a small example according to the provided stackoverlow link. It sure not the most elegant solution because the code is very static for your case but you can improve it. Explanation inside of the code.

myData = [{
    name: "Test",
    lastName:"1",
    moreData: [{
        left: 5,
        data: '',

     },
      {
        left: 3,
        data: '',

     },
     {
        left: 7,
        data: '',

     }
  ]



},
{
    name: "Test",
    lastName:"2",
    moreData: [{
        left: 8,
        data: '',

     },
      {
        left: 4,
        data: '',

     },
     {
        left: 9,
        data: '',

     }
  ]
}
];

Code:

myDataSplit = [];
//First we have to get ride of the multiple data in moreData
myData.forEach(level1 => {
    level1.moreData.forEach(level2 => {
        //Here we build a new array with a element for each moreData
        //If you want it more flexible push the whole level1 and replace moreData with level2
        myDataSplit.push({name: level1.name, lastName: level1.lastName, moreData: level2});
    });
});

//Now the compare like in the SO link
function compare(a,b) {
    if (a['moreData'].left < b['moreData'].left)
      return -1;
    if (a['moreData'].left > b['moreData'].left)
      return 1;
    return 0;
  }

  myDataSplit.sort(compare);
  console.log(myDataSplit);

The result is like you want: ​​​​

​​​​Quokka #2 (node: v9.5.0)​​​​
​​​​​​​​​​
​​​​​[ { name: 'Test', lastName: '1', moreData: { left: 3, data: '' } },​​​​​
​​​​​  { name: 'Test', lastName: '2', moreData: { left: 4, data: '' } },​​​​​
​​​​​  { name: 'Test', lastName: '1', moreData: { left: 5, data: '' } },​​​​​
​​​​​  { name: 'Test', lastName: '1', moreData: { left: 7, data: '' } },​​​​​
​​​​​  { name: 'Test', lastName: '2', moreData: { left: 8, data: '' } },​​​​​
​​​​​  { name: 'Test', lastName: '2', moreData: { left: 9, data: '' } } ]​​​​​
  at ​​​myDataSplit​​​ ​quokka.js:61:2​
Doomenik
  • 868
  • 1
  • 12
  • 29
  • With other data, this does not split them well. As I get : Name: LastName moreData: [ {}, {}, {}] --> multiple obejcts in more data. Isn't the purpose of your code to split multiple objects into one for each object ? – JennyJ Mar 19 '18 at 11:59
  • @JennyJ Yes you can add as many moreData as you want. Tried it by myself and worksfine. – Doomenik Mar 19 '18 at 12:04
  • But I mean it doesn't split into an object for each object that's inside moreData. Instead I get the same thing that I had in the beginning – JennyJ Mar 19 '18 at 12:06
  • @JennyJ If you look in the result its exactly like your example, 2 objects get split for each moreData and sorted by the attribute left. I added 1 more moreData and like you see you have now 6 elements in the result. – Doomenik Mar 19 '18 at 12:09
  • Ah I apologize! I was passing the entire level1 ( which would include the moreData, and made it confusing) Thank you so much!! – JennyJ Mar 19 '18 at 12:14
  • @JennyJ No problem, glad I could help. – Doomenik Mar 19 '18 at 12:15