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?

4
  • 2
    Welcome to Stackoverflow, your question has already be asked multiple times. Before posting a question I recommend using the search function. Something like stackoverflow.com/questions/1129216/… should answer your question Commented Mar 19, 2018 at 10:35
  • Possible duplicate of Sort array of objects by string property value in JavaScript Commented Mar 19, 2018 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. Commented Mar 19, 2018 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. Commented Mar 19, 2018 at 10:54

1 Answer 1

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​
Sign up to request clarification or add additional context in comments.

6 Comments

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 Yes you can add as many moreData as you want. Tried it by myself and worksfine.
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 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.
Ah I apologize! I was passing the entire level1 ( which would include the moreData, and made it confusing) Thank you so much!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.