-1

I have this array of object in typescript

[
  {target_col: "`qsze`", operation: "", isNew: false, operationNotCompiled: "", index: 25},
  {target_col: "`qsf`", operation: "", isNew: false, operationNotCompiled: "", index: 26},
  {target_col: "`amiu`", operation: "'jnhghghgh'", isNew: true, operationNotCompiled: "'jnhghghgh'",…},
  {target_col: "`amiu`", operation: "", isNew: false, operationNotCompiled: "", index: 27}
]

and I want to remove duplicate elements.

I want if there is no element duplicated return the object like it is. else if there is elements duplicated remove the one that have operationNotCompiled="".

if there elements duplicated and the 2 elements have operationNotCompiled=""then remove one.

2 elements duplicated=====>>> 2 elements have the same attribute target_col. like this 2.

{target_col: "`amiu`", operation: "'jnhghghgh'", isNew: true, operationNotCompiled: "'jnhghghgh'",…},
{target_col: "`amiu`", operation: "", isNew: false, operationNotCompiled: "", index: 27}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
oumaima
  • 61
  • 1
  • 2
  • 3
  • https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array/9229821#9229821 – kulaeff Jan 21 '20 at 17:28

1 Answers1

0

welcome oumaima!

Here is an example:

const arr = [
  {target_col: "`qsze`", operation: "", isNew: false, operationNotCompiled: "", index: 25},
  {target_col: "`qsf`", operation: "", isNew: false, operationNotCompiled: "", index: 26},
  {target_col: "`amiu`", operation: "'jnhghghgh'", isNew: true, operationNotCompiled: "'jnhghghgh'"},
  {target_col: "`amiu`", operation: "", isNew: false, operationNotCompiled: "", index: 27}
]

function checkDuplicates(arr) {
  keys = []
  keysDuplicates = [] 
  arr.map(item => {
    if (keys.indexOf(item.target_col) == -1) { // if we see a key first time - put it in keys
      keys.push(item.target_col)
    } else { // otherwise put it in duplicated keys
      keysDuplicates.push(item.target_col)
    }
  })
  if (keysDuplicates.length) {
    return keysDuplicates.map(key => arr.filter(item => item.target_col === key))
  }
  
  return arr
}

console.log(checkDuplicates(arr));
qiAlex
  • 4,290
  • 2
  • 19
  • 35
  • in result i want this:===>[ {target_col: "`qsze`", operation: "", isNew: false, operationNotCompiled: "", index: 25}, {target_col: "`qsf`", operation: "", isNew: false, operationNotCompiled: "", index: 26}, {target_col: "`amiu`", operation: "'jnhghghgh'", isNew: true, operationNotCompiled: "'jnhghghgh'"} ] – oumaima Jan 21 '20 at 18:11
  • this website is about to help you evolve, to help you to understand how to solve a problem, not doing exactly your specific tasks. You were provided with fair enough clues. Show me your *best try* how to achieve your specific needs. – qiAlex Jan 22 '20 at 00:17