Reduce problem

Someone that has a clean solution for this? I have this data structure:
posts: Post[]

interface Post {
stream : {
id: string
name: string
}
isCompleted: boolean
// other data...
}
posts: Post[]

interface Post {
stream : {
id: string
name: string
}
isCompleted: boolean
// other data...
}
I want to get a response with two data structures (maps) mapped by stream name that separates completed streams and uncompleted streams where the keys are the stream names. A stream is considered completed if all the posts in that stream are completed. I guess a reduce would be the way to go but would need help with some specifics.
completedStreams: {
// keys are the names of the completed streams and inside is an array with // the posts
}
uncompletedStreams: {
// keys are the names of the unCompleted streams and inside is an array with // the posts for these streams
}
completedStreams: {
// keys are the names of the completed streams and inside is an array with // the posts
}
uncompletedStreams: {
// keys are the names of the unCompleted streams and inside is an array with // the posts for these streams
}
32 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
Hmm almost but now the posts will be added to completed if the post is completed and to uncompleted if it isn't. It doesn't take into account if all of the posts in that stream are completed or not right?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
data like this for example:
[
{
id: 1,
isCompleted: true,
stream: {
id: 1,
name: "m"
}
},
{
id: 2
isCompleted: true,
stream: {
id: 1,
name: "m"
}
},
{
id: 3
isCompleted: true,
stream: {
id: 1,
name: "m"
}
},
{
id: 4
isCompleted: false,
stream: {
id: 2,
name: "n"
}
},
{
id: 5
isCompleted: true,
stream: {
id: 2,
name: "n"
}
},
]
[
{
id: 1,
isCompleted: true,
stream: {
id: 1,
name: "m"
}
},
{
id: 2
isCompleted: true,
stream: {
id: 1,
name: "m"
}
},
{
id: 3
isCompleted: true,
stream: {
id: 1,
name: "m"
}
},
{
id: 4
isCompleted: false,
stream: {
id: 2,
name: "n"
}
},
{
id: 5
isCompleted: true,
stream: {
id: 2,
name: "n"
}
},
]
This should result in
completed: {
m: [
{
id: 1,
isCompleted: true,
stream: {
id: 1,
name: "m"
}
},
{
id: 2
isCompleted: true,
stream: {
id: 1,
name: "m"
}
},
{
id: 3
isCompleted: true,
stream: {
id: 1,
name: "m"
}
},
]

uncompleted: {
n: {
{
id: 4
isCompleted: false,
stream: {
id: 2,
name: "n"
}
},
{
id: 5
isCompleted: true,
stream: {
id: 2,
name: "n"
}
},
}
completed: {
m: [
{
id: 1,
isCompleted: true,
stream: {
id: 1,
name: "m"
}
},
{
id: 2
isCompleted: true,
stream: {
id: 1,
name: "m"
}
},
{
id: 3
isCompleted: true,
stream: {
id: 1,
name: "m"
}
},
]

uncompleted: {
n: {
{
id: 4
isCompleted: false,
stream: {
id: 2,
name: "n"
}
},
{
id: 5
isCompleted: true,
stream: {
id: 2,
name: "n"
}
},
}
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
I had a really ugly solution at first but I wasn't happy with that. I basically created one map with all posts, one map with all completed posts and one map with all uncompleted posts grouped by stream name. To check if it was a completed stream I just checked if the length of the same key in the completed post array for that key was as long as it was for allpost array for that key.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
yeah only issue I guess is that I don't get it mapped by stream name/id with that solution I just get all the posts for all the completed streams in one array and all the ones for uncompleted in the other one I guess or is the type wrong in the end? 🤔
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
The posts still contain their original data. that is not mutated but the stream is considered uncompleted since all the posts in that stream aren't completed
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
@OrJDev thanks a lot for the help. That seems to be pretty much what I'm looking for! Closer to what I had done myself though but yours might be quite a lot more efficient.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
Do you think this could be simplified further with a reduce btw or is this as low time complexity as possible? 🤔
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
because this is O(n^2) right? @matee yeah using a reduce and Maps was what I thought would be the best solution but I'm not sure how to execute it.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
The time complexity for this is relatively important because there could possibly be 1000s of posts where each post is a document in db
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
and 5000x5000 and it's a lot of documents 😅
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
@OrJDev isn't his logic just O(n) @matee Thanks a lot, is the time complexity of that just n? 😮
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
yeah I think so as well, really clean solution and thanks a lot to the both of you. I'm so glad places like this exist ❤️
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
yeah I think Maps should be fine tbh. I'm just gonna use the keys to display the names of the streams and values to display the posts This is done on the first screen in my app so that's why it's important to get it right 😄 Would you memoize the results of this btw? 🤔
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
How do I best map through a map btw while still being able to use the key and values separately? Would this be a good solution to render components based on uncompleted streams?
const unCompletedStreamSummaries = () => {
let streamSummaries: JSX.Element[] = []
for (const [key, value] of uncompleted.entries()) {
streamSummaries.push(
<StreamSummary
key={key}
streamName={key}
posts={value}
streamId={value[0]?.stream?.id ?? ''}
totalPosts={value?.length}
completedPosts={value?.filter(post => post.isCompleted).length}
/>,
)
}
return streamSummaries
}
const unCompletedStreamSummaries = () => {
let streamSummaries: JSX.Element[] = []
for (const [key, value] of uncompleted.entries()) {
streamSummaries.push(
<StreamSummary
key={key}
streamName={key}
posts={value}
streamId={value[0]?.stream?.id ?? ''}
totalPosts={value?.length}
completedPosts={value?.filter(post => post.isCompleted).length}
/>,
)
}
return streamSummaries
}
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
Oh yeah that's a way cleaner solution
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Wezter
WezterOP3y ago
Didn't really figure out a solution with that that wouldn't become at least O(n^2)
Want results from more Discord servers?
Add your server