vue.js实现数组去重的方法:使用两个for循环来判断每一项的id,如【that.positions.map(train=>{that.new_Positions.push( train.trainId)})that.resul...】。

本文操作环境:windows10系统、vue 2.5.2、thinkpad t480电脑。
vue.js中实现数组去重可以考虑使用for循环和... new set两种方式来实现,一起来看下吧!
第一种方法:
用2个for循环,判断每一项的id
立即学习“前端免费学习笔记(深入)”;
具体代码如下:
// that.positions.map(train=>{
// that.new_Positions.push( train.trainId)
// })
// that.resultArr = [];//去重后的数组
// var flag;
// for (var i in that.new_Positions){
// flag = true;
// for (var j in that.resultArr) {
// if (that.resultArr[j] == that.new_Positions[i]) {
// flag = false;
// break;
// }
// }
// if (flag) {
// that.resultArr.push(that.new_Positions[i]);
// }
// }
// console.log("that.resultArr:",that.resultArr)打印的结果:

第二种方法:
用... new set 实现
具体代码如下:
that.positions.map(train=>{
that.new_Positions.push(train.trainId)
})
that.new_Positions = [...new Set(that.new_Positions)];
console.log("that.resultArr:",that.new_Positions)学习推荐:php培训
以上就是vue.js如何实现数组去重的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号