Remove duplicate element depending the user parameters in JS?

Viewed 61

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' ]}
1 Answers

I built a map instead with the possible matched words for each category, then for each category if it has more than one word filter each word depending on it's existence on other matched categories

const Themes = {
  Animals: [
    'ZOE',
    'ZEBRA'
  ],
  Names: [
    'ZOE'
  ]
}

let users = []
let increment = 0
let copyArr = []
let usedAnswers = [];

checkProposition()

function checkProposition() {
  users.push({
    id: '1',
    words: ['ZOE', 'ZEBRA'],
    score: 0
  }, {
    id: '2',
    words: ['ZOE', 'ZOE'],
    score: 0
  },
  {
    id: '3',
    words: ['ZOE'],
    score: 0
  })
  users.forEach(user => {
    usedAnswers[increment] = {}
    copyArr.push({
      id: user.id,
      wordsFound: [],
      saveCategory: []
    })
    user.words.forEach(word => {
      algorithmCore(word, usedAnswers[increment])
    })

    for (var key in usedAnswers[increment]) {
      if (usedAnswers[increment][key].length > 1) {
        usedAnswers[increment][key] = usedAnswers[increment][key].filter(word => {
          for (var key1 in usedAnswers[increment]) {
            if (key !== key1 && usedAnswers[increment][key1].includes(word))
              return false;
          }
          return true;
        })
      }
    }
    
    const userWords=[...user.words];
    Object.entries(usedAnswers[increment]).forEach(([key,words])=>{
      const wordIndex=userWords.indexOf(words[0]);
      if(wordIndex >= 0){
        userWords.splice(wordIndex,1);
        copyArr[increment].wordsFound.push(words[0])
        copyArr[increment].saveCategory.push(key)
      }
    })

    increment++
  })
  console.log(copyArr)
}

function algorithmCore(word, used) {
  for (var key in Themes) {
    for (var answer in Themes[key]) {
      const wordKey = `${word}(${key})`;
      if (word == Themes[key][answer]) {
        used[key] = (used[key] || []);
        if (!used[key].includes(word))
          used[key].push(word)
      }
    }
  }
  return
}

Related