<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        es6 filter() 數組過濾方法總結

        來源:懂視網 責編:小采 時間:2020-11-27 21:59:23
        文檔

        es6 filter() 數組過濾方法總結

        es6 filter() 數組過濾方法總結:Array.every(x=>x)是每一個都要滿足 Array.some(x=>x)是有一個滿足。 Array.find(findIndex),返回符合條件的第一個值。 Array.filter(過濾成新的數組) 數組的方法分為兩類 1)改變原數組 push,pop,shift,unshift,sor
        推薦度:
        導讀es6 filter() 數組過濾方法總結:Array.every(x=>x)是每一個都要滿足 Array.some(x=>x)是有一個滿足。 Array.find(findIndex),返回符合條件的第一個值。 Array.filter(過濾成新的數組) 數組的方法分為兩類 1)改變原數組 push,pop,shift,unshift,sor

        Array.every(x=>x)是每一個都要滿足

        Array.some(x=>x)是有一個滿足。

        Array.find(findIndex),返回符合條件的第一個值。

        Array.filter(過濾成新的數組)

        數組的方法分為兩類

        1)改變原數組

        push,pop,shift,unshift,sort,reverse,splice

        2)不改變原數組concat,join-->

        split,toStringpush:從數組最后一位開始加數據

        pop:把數組最后一位剪切

        shift:在數組最前一位剪切

        unshift:在數組最前一位加數

        reverse:把原數組逆轉

        splice:arr.splice(從第幾位開始,截取多少長度,在切口處添加新數據)

        concat :連接join:返回字符串

        slice:截取arr.slice(從該為開始截取,截取到該為)

        示例

        1.創建一個數組,判斷數組中是否存在某個值

        var newarr = [
         { num: 1, val: 'ceshi', flag: 'aa' },
         { num: 2, val: 'ceshi2', flag: 'aa2' }
        ]
        console.log(newarr.filter(item => item.num===2 ))

        2.也可以通過上面方法過濾掉num為2的留下num為1的

        var newarr = [
         { num: 1, val: 'ceshi', flag: 'aa' },
         { num: 2, val: 'ceshi2', flag: 'aa2' }
        ]
        console.log(newarr.filter(item => item.num!=2 ))

        3.去掉空數組空字符串、undefined、null

        var arr = ['1','2',undefined, '3.jpg',undefined]
        var newArr = arr.filter(item => item)
        console.log(newArr)
        
        var arr = ['1','2',null, '3.jpg',null]
        var newArr = arr.filter(item => item)
        console.log(newArr)
        
        >//空字符串里面不能包含空格
        var arr = ['1','2','', '3.jpg','']
        var newArr = arr.filter(item => item)
        console.log(newArr)

        4.去掉數組中不符合項

        var arr = [20,30,50, 96,50]
        var newArr = arr.filter(item => item>40) 
        console.log(newArr)

        5.過濾不符合項

        var arr = ['10','12','23','44','42']
        var newArr = arr.filter(item => item.indexOf('2')<0) 
        console.log(newArr)

        6.數組去重

        var arr = [1, 2, 2, 3, 4, 5, 5, 6, 7, 7,8,8,0,8,6,3,4,56,2];
        var arr2 = arr.filter((x, index,self)=>self.indexOf(x)===index) 
        console.log(arr2); //[1, 2, 3, 4, 5, 6, 7, 8, 0, 56]

        7

        /*
         有一個對象數組 a ,將a數中對象某個屬性的值存儲到B數組中
        */ 
        var porducts = [
         {name:"cucumber",type:"vegetable"},
         {name:"banana",type:"fruit"},
         {name:"celery",type:"vegetable"},
         {name:"orange",type:"fruit"},
        ];
        // es5
        var filteredProducts = [];
        for(var i = 0;i < porducts.length; i ++){
         if(porducts[i].type === "fruit"){
         // 如果條件滿足就把當前的值推入
         filteredProducts.push(porducts[i])
         }
        }
        // console.log(filteredProducts)//0: {name: "banana", type: "fruit"}1: {name: "orange", type: "fruit"}length: 2__proto__: Array(0)
        // ES6
         var filter2 = porducts.filter(function(porduct){//對porducts數組對象過濾如果porduct.type === "fruit"就return出去,再用一個變量接住
         return porduct.type === "fruit"
        })
        console.log(filter2)
        

        8

        /*
         需求二
         有一個對象數組A,過濾掉不滿足以下條件對象
         條件:蔬菜 數量大于0 價格小于10
        */ 
        var products = [
         {name:"cucumber",type:"vegetable",quantity:0,price:1},
         {name:"banana",type:"fruit",quantity:10,price:16},
         {name:"celery",type:"vegetable",quantity:30,price:8},
         {name:"orange",type:"fruit",quantity:3,price:6},
        ];
        products = products.filter(function(product){
         return product.type === "vegetable"
         && product.quantity > 0
         && product.price < 10
        })
        console.log(products)//0: {name: "celery", type: "vegetable", quantity: 30, price: 8}name: "celery"price: 8quantity: 30type: "vegetable"__proto__: Objectlength: 1__proto__: Array(0)
        

        9

        /*
         需求三:
         有兩個數組A,B,根據A中的ID值 ,過濾掉B數組不符合的數據
        */ 
        var post = {id:4,title:"javascript"};
        var comments = [
         {postId:4,content:'Angular4'},
         {postId:2,content:'VUE.js'},
         {postId:3,content:'Node.js'},
         {postId:4,content:'React.js'},
        ];
        function commentsForPost(post,comments){
         return comments.filter(function(comment){
         return comment.postId === post.id;
         })
        }
        console.log(commentsForPost(post,comments))
        // 0: {postId: 4, content: "Angular4"}1: {postId: 4, content: "React.js"}length: 2__proto__: Array(0)

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        es6 filter() 數組過濾方法總結

        es6 filter() 數組過濾方法總結:Array.every(x=>x)是每一個都要滿足 Array.some(x=>x)是有一個滿足。 Array.find(findIndex),返回符合條件的第一個值。 Array.filter(過濾成新的數組) 數組的方法分為兩類 1)改變原數組 push,pop,shift,unshift,sor
        推薦度:
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 99爱视频99爱在线观看免费| 久久国产精品免费| 亚洲av午夜精品无码专区| 亚洲男人电影天堂| 亚洲日本一线产区和二线| 成人午夜影视全部免费看| 精品亚洲永久免费精品| 亚洲精品视频免费在线观看| 日韩免费观看视频| 亚洲精品永久www忘忧草| 91av在线免费视频| 亚洲无人区一区二区三区| 亚洲综合校园春色| 中文字幕免费在线播放| 天天看片天天爽_免费播放| 中文字幕久久亚洲一区| 亚洲字幕AV一区二区三区四区| 久久久精品国产亚洲成人满18免费网站 | 亚洲国产精品无码AAA片| 亚洲日韩乱码中文字幕| 成人爱做日本视频免费| 日本一区二区三区免费高清在线 | 大地资源二在线观看免费高清| 亚洲日本va在线视频观看| 免费看搞黄视频网站| 亚洲人成在线播放| 免费网站看av片| 亚洲精品午夜在线观看| 日本免费v片一二三区| 高清永久免费观看| 亚洲最大黄色网站| 亚洲视频免费在线看| 亚洲Av永久无码精品一区二区| 日韩精品免费一级视频| 香蕉视频在线观看亚洲| www成人免费观看网站| 亚洲精品亚洲人成在线观看下载| 亚洲中文字幕无码av永久| 丁香亚洲综合五月天婷婷| 国产精品亚洲一区二区三区久久 | 亚洲综合色一区二区三区小说|