数组去重问题

https://img1.mukewang.com/5c418c800001b14e05390716.jpg

这样一个数组,需要循环遍历吧Time相同的放入一个新数组

Helenr
浏览 396回答 1
1回答

忽然笑

var arrNew = [];for (var i = 0; i < arr.length; i++) {&nbsp; &nbsp; var len = arr.length;&nbsp; &nbsp; for (var j = i + 1; j < arr.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if(arr[i].Time === arr[j].Time){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arrNew.push(arr[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr.splice(j,1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j--&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if(len !== arr.length){&nbsp; &nbsp; &nbsp; &nbsp; arrNew.unshift(arr[i]);&nbsp; &nbsp; }}突然想到一种更简洁的方法var arrNew = arr.filter(function(x){&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(x.Time.indexOf(arr[i].Time) !== -1 && x !== arr[i]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return x;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript