Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to map array of objects and return them as a new array of objects sorted in "categories" [duplicate]

$
0
0

I'm building my first react-native app and I want to show user some screen of his "followers" separated by categories but to do it I think I have to create new array of objects from this what I have.

I have array of objects like this:

followers: [    {      userId: 2,      userName: "Abigail",      followDate: "1980-04-09T10:15:30+07:00",      category: "A"    },    {      userId: 3,      userName: "John",      followDate: "1980-04-09T10:15:30+07:00",      category: "B"    },    {      userId: 4,      userName: "Bob",      followDate: "1980-04-09T10:15:30+07:00",      category: "A"    },    {      userId: 5,      userName: "Martha",      followDate: "1980-04-09T10:15:30+07:00",      category: "B"    } ]

And now I would like to render these followers to user separated by categories that are in followers array objects. So I think I need to create new array of objects and I think it should looks like this:

followersSortedByCategory = [      {        category: "A",        followers: [          {            userId: 2,            userName: "Abigail",            followDate: "1980-04-09T10:15:30+07:00",            category: "A"          },          {            userId: 4,            userName: "Bob",            followDate: "1980-04-09T10:15:30+07:00",            category: "A"          }        ]      },      {        category: "B",        followers: [          {            userId: 3,            userName: "John",            followDate: "1980-04-09T10:15:30+07:00",            category: "B"          },          {            userId: 5,            userName: "Martha",            followDate: "1980-04-09T10:15:30+07:00",            category: "B"          }        ]      }    ]

I'm not sure how to do this without many "if's". I think there is a good way to do it using map, filter etc. functions but I don't know how.


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>