美文网首页
JavaScript 数组去重

JavaScript 数组去重

作者: 歌声缓缓 | 来源:发表于2021-03-16 09:26 被阅读0次

    使用es5

    var arr=[1,2,3,4,5,6,3,4,1,2,3,5,"a","b","c","a"];

    var new_arr=[];

    var new_obj={};

    for(var i=0;i<new_arr.length;i++){

    if(!new_obj.arr[i]){

    new_arr.push(arr[i])

    new_obj.arr[i]=true;

    } }

    console.log(temp_arr)  // [1, 2, 3, 4, 5, 6, "a", "b", "c"] 


    使用es6

    var arr=[1,2,3,4,5,6,3,4,1,2,3,5,"a","b","c","a"];

    var newarr=new Set(arr);

    console.log(newarr) // [1, 2, 3, 4, 5, 6, "a", "b", "c"] 

    相关文章

      网友评论

          本文标题:JavaScript 数组去重

          本文链接:https://www.haomeiwen.com/subject/zbqkcltx.html