taiyangshenniao 2019-06-26
>>> a = [9,8,7,9,7,1,2,1,2,5,3] >>> new_a = list(set(a)) >>> new_a [1, 2, 3, 5, 7, 8, 9] #此时new_a未保持原有的顺序,对new_a进行排序 >>> new_a.sort(key = a.index) >>> new_a [9, 8, 7, 1, 2, 5, 3]
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript"> var array1 = [1,2,6,8,4,2,3,9,47,1,2,23,5,8,3]; var result = []; for (var i = 0; i < array1.length; i++) { if (array1.indexOf(array1[i])==i) //如过该元素在数组中第一次出现的位置 == 该元素当前的位置【A】 { result.push(array1[i]);//将符合【A】条件的元素加入到result中 } } alert(result); </script> </head> <body> </body> </html>