Get most matched Classes
const intersection = yourArray.filter(item1 => yourOtherArray.some(item2 => item2 === item1))
25 Replies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
You're doing == comparison on the entire array rather than its elements.
Filtering array == arrayItem is never going to work right there.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Something like that.
It should then have the most matching item as the first item in the array.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
so if you want the most matched classes not ALL matches this'll do:
the thing is that doesn't match the code you actually have
I know you already got an answer
I just wanted to add this way since there's some more efficiency there and this strictly speaking answers the initial question.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Honestly for small arrays you should see no difference
Also doesn’t check every
You can do this in O(n) time though
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
The thing with
filter
, map
etc. is that they loop over the whole array and make a new one, e.g. for something like a.map(item => item + 1).map(item => item + 1).map(item => item + 1)
it's gonna make several arrays
some browsers will optimise things but there's no guarantees
some languages have guarantees that these stream but that's besides the pointUnknown User•3y ago
Message Not Public
Sign In & Join Server To View
I'd only worry though for like arrays >1k size
and also profile
because browsers know they create new arrays, technically, but might optimise them away
might
you can do it like this if you want to get the result in one iteration of the array and minimal allocation:
but as you can probably tell that's not awfully readable and
matchCount
being, well, a count does assume no duplicates etc., i.e. if "window-app"
shows up 3 times it'll throw off the match count code
but this way you're guaranteed only one iteration of icons
.
I wouldn't really worry about actually using this though. Because the key insight in my opinion is frankly that this code is less readable and understandable at a glance.
iterating an array of size like 1 million shouldn't even take a second too soooo yeah
*correctness not guaranteed as I typed this out without actually running itUnknown User•3y ago
Message Not Public
Sign In & Join Server To View
Gotcha sorry I mistook something you said as a correction
I thought an item had to match everything in elemClassList
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
right, why are more classes better? Why not combine every class I suppose is what I'm saying.
that may have obvious flaws
but I'm asking mostly because if things are vague
the user can only pick with like "actor" how do you know they want "actor", "npc" and not "actor", "vehicle" or just "actor"
the code I gave will pick "actor", "npc" because it favors first and foremost the most matches, then the most classes, and then earlier ones in the array
but a user may favor any of those 3, right?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
it's totally fine if this code makes an intuitive UI
I was just trying to make sure that I couldn't reveal any other strategies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ah well since you doubt people'll use it too much why don't you come back if you find out people are using it lots
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ah nice seems convenient potentially
I wouldn’t worry too much about the UX of customizing what seems to be a relatively niche configuration but I see the desire to find the one with most matched classes now
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Add a tour.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View