0

This is how my solution should look like:

enter image description here

Right now I have an array where I group them all by there first letter like so

const grouped = _.groupBy(topicPages, key => key.relatedTopic.title[0]);

And this is the result :

enter image description here

enter image description here

So I am trying to figure out a way to display the Letter A with the titles that are in that array.

I have tried using some sort of map :

grouped.map(first, key) => {
  // here I want to display the key

 // and then continue looping over 
  first.map(title => {
    title.relatedTopic.title
  })
})
6
  • 1
    _.forEach() over it, since you're using Lodash. Or use any of the other Lodash methods that work here like _.entries() or something. Or loop over Object.entries() without Lodash. How you display those results seems like a separate issue, though. Commented Apr 15, 2021 at 9:23
  • 1
    Please replace the images with a text example? Commented Apr 15, 2021 at 9:25
  • you mentioned > I have tried using some sort of map , then? Commented Apr 15, 2021 at 9:27
  • I get : TypeError: grouped.map is not a function Commented Apr 15, 2021 at 9:28
  • Thanks, I will update my question to try and describe my problem better! Commented Apr 15, 2021 at 9:34

1 Answer 1

2

I get : TypeError: grouped.map is not a function

grouped is an object, which does not have a map function.

const input = ["All","Andre","Ben","Boy"];

const grouped = _.groupBy(input, item => item[0]);

console.log(grouped)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

In order to loop over it you probably want the entries

const input = ["All","Andre","Ben","Boy"];

const grouped = _.groupBy(input, item => item[0]);

_.forEach(_.entries(grouped), ([key,values]) => {
    console.log(key);
    values.forEach(value => {
       console.log("\t" + value);
    })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

Sign up to request clarification or add additional context in comments.

5 Comments

@PeterSeliger you could imply their structure from key.relatedTopic.title in the question, but this answer does not need to understand their structure - it describes the required implementation of iterating a grouped object
_.forEach already loops over keys and values of an object. But puts the key as second parameter. Your code is equivalent to _.forEach(grouped, (values, key) => /*no further changes needed*/ Demo
Thanks @VLAZ that's what I should have done
@Jamiec Can I ask why key and value is wrapped inside brackets? I can't figure out what is does
@EirikVattøy it destructures the array. _.entries returns an array like [key.value] and that just destructures it to variables key and values. More info here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

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.