我在打字稿中有这个对象数组
[
{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}
]
并且我想删除重复的元素。
我想如果没有重复的元素,则返回该对象。否则,如果元素重复,则删除具有operationNotCompiled=""
的元素。
[如果有重复的元素并且2个元素具有operationNotCompiled=""
,则删除一个。
2个元素重复===== >>> 2个元素具有相同的属性target_col。像这样2。
{target_col: "`amiu`", operation: "'jnhghghgh'", isNew: true, operationNotCompiled: "'jnhghghgh'",…},
{target_col: "`amiu`", operation: "", isNew: false, operationNotCompiled: "", index: 27}
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) {
keys.push(item.target_col)
} else {
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));