陣列 重複不新增 、相同刪除,相同賦值

weixin_30517001發表於2017-09-07
<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>

    </head>

    <body>
        <!--<script type="text/javascript">
            var selectArr = [{
                name: "xx",
                num: 1
            }];

            function checkSelArr(selected) {

                var len = selectArr.length;

                for(var i = 0; i < len; i++) {
                    if(selected.name == selectArr[i].name) {
                        //                        selectArr.splice(i, 1);

                        return;
                    }
                };
                selectArr.push(selected)

            }

            checkSelArr({
                name: "xx",
                num: 1
            }, {

            })
            checkSelArr({
                name: "zlt",
                num: 1
            }, {

            })
            var s = selectArr
            console.log(s)
            ///相同 不新增
        </script>-->

        <!--<script type="text/javascript">
            var selectArr = [{
                name: "xx",
                num: 1
            }, {
                name: "zlt",
                num: 12
            }];

            function checkSelArr(selected) {

                var len = selectArr.length;

                for(var i = 0; i < len; i++) {
                    if(selected.name == selectArr[i].name) {
                        //                        selectArr.splice(i, 1);
                        console.log(selectArr[i].num = selected.num)

                    }
                };

            }

            var s = selectArr
            checkSelArr({
                name: "xx",
                num: 888
            })
            console.log(s)
            //找到相同改變num
        </script>-->










    <script type="text/javascript">
            var selectArr = [{
                name: "xx",
                num: 1
            }];

            function checkSelArr(selected) {

                var len = selectArr.length;

                for(var i = 0; i < len; i++) {
                    if(selected.name == selectArr[i].name) {
                                        selectArr.splice(i, 1);

                        return;
                    }
                };
                

            }

            checkSelArr({
                name: "xx",
                num: 1
            })
            
            var s = selectArr
            console.log(s)
            ///刪除相同元素
        </script>

 

轉載於:https://www.cnblogs.com/nns4/p/7489619.html

相關文章