0

I've been trying to sort an array by a value within a nested array, data.Count. Unfortunately, I cannot seem to access it with lodash sortBy. Is there a better way to approach this?

 example = () => {
   const exampleArray = [];


   _.forEach(this.state.example[name], (data) => {
       exampleArray.push({name, data });
   })

    // these logs all work fine
    console.log(exampleArray); 
    console.log(exampleArray[0]);
    console.log(exampleArray[0].data.count); // prints out 4

    //****console error: data is undefined
    _.sortBy(exampleArray, [name, data.count]);

    return exampleArray
}
2
  • Possible duplicate of Lodash sort collection based on external array Commented May 10, 2018 at 17:03
  • not an external array. I'm trying to sort an array by a specific attribute which is contained inside yet another array object which is still contained into the overall array. Commented May 10, 2018 at 17:27

0

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.