Reputation: 11
without _.uniq
, as it doesn't work on my platform (autocode)
I tried filtering using ==
, but it didn't work.
I also tried _.uniq
, which didn't work either.
Code:
var tags = [...new Set(bot.tags)]
for (var i = 0; i < bot.tags.length; i++) {
tags1 += `\`${tags[i].label}\` `;
tags2.push({
label: `${tags[i].label}`,
value: `${tags[i].name}`,
default: false,
});
}
console.log(tags2);
Current value of tags2
:
[
{ label: 'Anime', value: 'anime', default: false },
{ label: 'Social', value: 'social', default: false },
{ label: 'Moderation', value: 'moderation', default: false },
{ label: 'Fun', value: 'fun', default: false },
{ label: 'Anti-scam', value: 'anti-scam', default: false },
{ label: 'anti-scam', value: 'anti-scam', default: false },
{
label: 'Multiple Languages',
value: 'multiple-languages',
default: false
},
{ label: 'Minecraft', value: 'minecraft', default: false },
{ label: 'Fun', value: 'fun', default: false },
{ label: 'Moderation', value: 'moderation', default: false },
{ label: 'Logging', value: 'logging', default: false },
{ label: 'anti-nuke', value: 'anti-nuke', default: false },
{ label: 'Utility', value: 'utility', default: false },
{ label: 'Multi-language', value: '-multi-language', default: false },
{ label: 'Anime', value: 'anime', default: false },
{ label: 'Logging', value: 'logging', default: false },
{ label: 'Together', value: 'together', default: false },
{ label: 'Utility', value: 'utility', default: false },
{ label: 'anti raid', value: 'anti-raid', default: false },
{
label: 'DiscordTogether',
value: 'discordtogether',
default: false
},
{ label: 'Social', value: 'social', default: false }
]
It seems that there are a few values with double value
, although they have a different label
. How can I fix that?
(I need to sort not by uniqueness of the whole object - only by it's value
)
Upvotes: -1
Views: 1244
Reputation: 1
var unique = tags2.filter((set => f => !set.has(f.value) && set.add(f.value))(new Set));
console.log(unique);
(set => f => !set.has(f.value) && set.add(f.value))
is a higher-order function that takes a Set
(set
) and returns a filtering function (f => !set.has(f.value) && set.add(f.value)
).
(new Set)
creates a new Set
and immediately passes it as an argument to the higher-order function, creating a closure with the Set
inside.
The returned filtering function is then used as the callback function for the filter
method on the tags2
array.
The filtering function checks if the Set
(set
) does not have the current element's value (!set.has(f.value)
) and, if true, adds the value to the Set
(set.add(f.value)
). This ensures that only unique values are kept in the Set
.
The filter
method returns a new array (unique
) containing only the elements that passed the filtering condition.
Upvotes: 0
Reputation: 16
I tried this on autocode and it's working.
var unique = tags2.filter((set => f => !set.has(f.value) && set.add(f.value))(new Set));
console.log(unique);
Upvotes: 0
Reputation: 73
if you are using javaScript you can use Set
A set is a collection of items which are unique i.e no element can be repeated. Set in ES6 are ordered: elements of the set can be iterated in the insertion order. Set can store any types of values whether primitive or objects.
// Use to remove duplicate elements from the array
const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]
//spreading numbers of the object into an array using the new operator
console.log([...new Set(numbers)])
// [2, 3, 4, 5, 6, 7, 32]
Upvotes: 1