我在打字稿中有这个对象数组
[
{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=""
的元素。
如果有重复的元素并且两个元素都有operationNotCompiled=""
,则删除一个。
2个元素重复===== >>>> 2个元素具有相同的属性target_col。 像这样2。
{target_col: "`amiu`", operation: "'jnhghghgh'", isNew: true, operationNotCompiled: "'jnhghghgh'",…},
{target_col: "`amiu`", operation: "", isNew: false, operationNotCompiled: "", index: 27}
答案 0 :(得分:0)
欢迎oumaima!
这里是一个示例:
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));