Welcome to Qi-U Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

js对象数组中删除出现次数为偶数次的项,保留奇数次的项

在对象数组中删除出现次数为偶数次的项,保留奇数次的项

var a = [
    { id: 1, content: '11' }, { id: 2, content: '22' }, { id: 1, content: '11' }, 
    { id: 2, content: '22' }, { id: 3, content: '33' },{ id: 4, content: '44' }, 
    { id: 4, content: '44' }, { id: 4, content: '44' }
]
 
//  这个时候数组a中  有四项为两两重复 有一项只出现一次  有一项重复了三次
//  现在想得到
a = [{ id: 3, content: '33' },{ id: 4, content: '44' }]
// 在数组中保留出现次数为奇数次的项  删除偶数次的项
 

有无大佬帮忙看看


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

准备一个新容器。
遍历旧数组的每一项

检查新容器里是否存在该项
若不存在则添加进新容器
若存在则从新容器中移除 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Qi-U Community for programmer and developer-Open, Learning and Share
...