I try to remove duplicates words in multiples categories. For this, I have an object named users which contains the words that user sends.
And I use a new object that contains the words found in dictionary with its category.
const Themes = {
Animals: [
'ZOE',
'ZEBRA'
],
Names: [
'ZOE'
]
}
let users = []
let increment = 0
let copyArr = []
checkProposition()
function checkProposition() {
users.push({
id: '1',
words: ['ZOE', 'ZEBRA'],
score: 0
})
users.forEach(user => {
copyArr.push({
id: user.id,
wordsFound: [],
saveCategory: []
})
user.words.forEach(word => {
algorithmCore(word)
})
increment++
})
console.log(copyArr)
}
function algorithmCore(word) {
for (var key in Themes) {
for (var answer in Themes[key]) {
if (word == Themes[key][answer]) {
copyArr[increment].wordsFound.push(word)
copyArr[increment].saveCategory.push(key)
}
}
}
return
}
Output copyArr
{ id: '1', wordsFound: [ 'ZOE', 'ZOE', 'ZEBRA' ], saveCategory: [ 'Animals', 'Names', 'Animals' ]}
Expected output :
{ id: '1', wordsFound: [ 'ZOE', 'ZEBRA' ], saveCategory: [ 'Names', 'Animals' ]}
And it should be : ZOE (Names) ZEBRA (Animals)
If User sends :
users.push({
id: '1',
words: ['ZOE', 'ZOE'],
score: 0
})
It should be :
{ id: '1', wordsFound: [ 'ZOE', 'ZOE' ], saveCategory: [ 'Names', 'Animals' ]}
But if user sends :
users.push({
id: '1',
words: ['ZOE', 'ZOE', 'ZEBRA'],
score: 0
})
It should be:
{ id: '1', wordsFound: [ 'ZOE', 'ZOE' ], saveCategory: [ 'Names', 'Animals' ]}
or
{ id: '1', wordsFound: [ 'ZOE', 'ZEBRA' ], saveCategory: [ 'Names', 'Animals' ]}