有趣的小案例

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            #big{
                width: 400px;
                height: 400px;
                border: 1px solid green;
                margin: 0 auto;
            }
            #small{
                width: 380px;
                height: 260px;
                overflow: auto;
                border: 1px solid #ccc;
                
            }
            #txt{
                width: 360px;
                height: 60px;
                margin-left: 10px;
                margin-top: 30px;
            }
            #btn{
                float: right;
                margin-top: 10px;
                margin-right: 50px;
            }
            li{
                list-style: none;
            }
            
        </style>
    </head>
    <body>
        <div id="big">
            <div id="small"></div>
            <textarea name="txt" rows="35" cols="6" id="txt"></textarea><br />
            <input type="button"  id="btn" value="发送" />
        </div>
    </body>
    <script src="../js/public.js"></script>
    <script>
        var txt=my$("txt");
        var small=my$("small");
        var ulobj=document.createElement("ul");
        ulobj.style.paddingLeft='20px'
        ulobj.style.paddingRight='20px'
        small.appendChild(ulobj);
        my$("btn").onclick=function(){
//            small.innerHTML=txt.value;
//            txt.value="";
         var liobj=document.createElement("li");
         ulobj.appendChild(liobj);
         liobj.innerHTML=txt.value;
         txt.value="";
         for(var i=0;i<ulobj.children.length;i++){
             if(i%2==0){
                 liobj.style.textAlign='left';
                 liobj.style.width="100%";
             }
             else{
                 liobj.style.textAlign='right';
                 liobj.style.width="100%";
             }
         }
        }
    </script>
</html>
==============================================================================================================================》聊天对话框

function changeColor(){
var r=random(0,255).toString(16);
var g=random(0,255).toString(16);
var b=random(0,255).toString(16);

return "#"+createZero(r)+createZero(g)+createZero(b);

}

function createZero(n){
return n.length<2?"0"+n:n;
}
var s=changeColor();
console.log(s);

==========================================================================================================================》生成十六进制颜色

function changeColor(){
var r=random(0,255);
var g=random(0,255);
var b=random(0,255);

return "rgb("+r+","+g+","+b+")";

}
var s=changeColor();
console.log(s);

==========================================================================================================================》生成rgb颜色

1.将数组的每一个元素依次与其他元素做比较,发现重复元素,删除

    var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5,5,5,5];
    console.log(arr);    //[1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5, 5, 5, 5]
    function noRepeat1(arr) {
        for(var i = 0; i < arr.length-1; i++){
            for(var j = i+1; j < arr.length; j++){
                if(arr[i]===arr[j]){
                    arr.splice(j,1);
                    j--;
                }
            }
        }
        return arr;
    }
    var arr2 = noRepeat1(arr);
    console.log(arr2);    //[1, 23, 3, 5, 6, 7, 9, 8]

2.借助indexOf()方法判断此元素在该数组中首次出现的位置下标与循环的下标是否相等

    var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5,5,5];
    console.log(arr);    //[1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5, 5, 5]
    function noRepeat2(arr) {
        for (var i = 0; i < arr.length; i++) {
            if (arr.indexOf(arr[i]) != i) {
                arr.splice(i,1);//删除数组元素后数组长度减1后面的元素前移
                i--;//数组下标回退
            }
        }
        return arr;
    }
    var newArr = noRepeat2(arr);
    console.log(newArr);    //[1, 23, 3, 5, 6, 7, 9, 8]

3.利用数组中的filter方法

    var arr = ['apple','banana','pear','apple','orange','orange'];
    console.log(arr)    //["apple", "banana", "pear", "apple", "orange", "orange"]
    var newArr = arr.filter(function(value,index,self){
    return self.indexOf(value) === index;
    });
    console.log(newArr);    //["apple", "banana", "pear", "orange"]

4.借助新数组 通过indexOf方判断当前元素在数组中的索引如果与循环的下标相等则添加到新数组中

    var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5,5,5];
    console.log(arr)    //[1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5, 5, 5]
    function noRepeat4(arr) {
        var ret = [];
        for (var i = 0; i < arr.length; i++) {
            if (arr.indexOf(arr[i]) == i) {
                ret.push(arr[i]);
            }
        }
        return ret;
    }
    var arr2 = noRepeat4(arr);
    console.log(arr2);    //[1, 23, 3, 5, 6, 7, 9, 8]

5.利用空对象来记录新数组中已经存储过的元素

    var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5];
    console.log(arr)    //[1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5]
    var obj={};
    var newArr=[];
    for(var i=0;i<arr.length;i++){
        if(!obj[arr[i]]){
            obj[arr[i]]=true;
            newArr.push(arr[i]);
        }
    }
    console.log(newArr);    //[1, 23, 3, 5, 6, 7, 9, 8]

6.借助新数组,判断新数组中是否存在该元素如果不存在则将此元素添加到新数组中

    var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5];
    console.log(arr);    //[1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5]
    function noRepeat6(arr){
        var newArr = [];
        for(var i = 0; i < arr.length; i++){
            if(newArr.indexOf(arr[i]) == -1){
                newArr.push(arr[i]);
            }
        }
        return newArr;
    }
    var arr2 = noRepeat6(arr);
    console.log(arr2);    //[1, 23, 3, 5, 6, 7, 9, 8]

7.借助新数组,判断新数组中是否存在该元素如果不存在则将此元素添加到新数组中(原数组长度不变但被按字符串顺序排序)

    var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5];
    console.log(arr);    //[1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5]
    function noRepeat7(arr) {
        var ret = [],
            end;//临时变量用于对比重复元素
        arr.sort();//将数重新组排序
        end = arr[0];
        ret.push(arr[0]);
        for (var i = 1; i < arr.length; i++) {
            if (arr[i] != end) {//当前元素如果和临时元素不等则将此元素添加到新数组中
                ret.push(arr[i]);
                end = arr[i];
            }
        }
        return ret;
    }
    var arr2 = noRepeat7(arr);
    console.log(arr2);    //[1, 23, 3, 5, 6, 7, 8, 9]

8.此方法没有借助新数组直接改变原数组,并且去重后的数组被排序

    var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5];
    console.log(arr);    //[1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5]
    function noRepeat8(arr) {
        var end;//临时变量用于对比重复元素
        arr.sort();//将数重新组排序
        end = arr[0];
        for (var i = 1; i < arr.length; i++) {
            if (arr[i] == end) {//当前元素如果和临时元素相等则将此元素从数组中删除
                arr.splice(i,1);
                i--;
            }else{
                end = arr[i];
            }
        }
        return arr;
    }
    var arr2 = noRepeat8(arr);
    console.log(arr2);    //[1, 23, 3, 5, 6, 7, 8, 9]

9.双层循环改变原数组

    var arr = [1,1,2,2,3,3,4,4,5,5,4,3,1,2,6,6,6,6];
    console.log(arr);    //[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 3, 1, 2, 6, 6, 6, 6]
    function noRepeat9(arr){
        for (var i = 0; i < arr.length; i++) {
            for (var j = 0; j < arr.length; j++) {
                if (arr[i] == arr[j] && i != j) {//将后面重复的数删掉
                    arr.splice(j, 1);
                }
            }
        }
        return arr;
    }
    var arr2  = noRepeat9(arr);
    console.log(arr2);    //[1, 2, 3, 4, 5, 6]

10.借助新数组

    var arr = [1,1,2,2,3,3,4,4,5,5,4,3,2,1,1,1];
    console.log(arr);    //[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 3, 2, 1, 1, 1]
    var newArr = [];
    for (var i = 0; i < arr.length; i++) {
        var repArr = [];//接收重复数据后面的下标
        //内层循环找出有重复数据的下标
        for (var j = i + 1; j < arr.length; j++) {
            if (arr[i] == arr[j]) {
                repArr.push(j);//找出后面重复数据的下标
            }
        }
        //console.log(repArr);
        if (repArr.length == 0) {//若重复数组没有值说明其不是重复数据
            newArr.push(arr[i]);
        }
    }
    console.log(newArr);    //[5, 4, 3, 2, 1]

11.借助ES6提供的Set结构

    var arr = [1,1,2,2,3,3,4,4,5,5,4,3,2,1,1,1];
    console.log(arr);    //[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 3, 2, 1, 1, 1]
    function noRepeat11(arr){
        var newArr = [];
        var myset = new Set(arr);//利用了Set结构不能接收重复数据的特点
        for(var val of myset){
            newArr.push(val)
        }
        return newArr;
    }
    var arr2 = noRepeat11(arr)
    console.log(arr2);    //[1, 2, 3, 4, 5]
==========================================================================================================================》十一种数组去重的方法

posted on 2019-08-24 15:31  月光下的阳宝  阅读(179)  评论(0编辑  收藏  举报