第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么這個數(shù)組.forEach 這個方法is underfind,如下圖和代碼

為什么這個數(shù)組.forEach 這個方法is underfind,如下圖和代碼

PHP
紅糖糍粑 2019-03-14 09:01:19
問題 我想得到數(shù)組里每一條數(shù)據(jù)的age>10 && age<30的數(shù)據(jù) 報錯 代碼 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> </body> </html> <script> var data = [{ id: 1, name: "test1", age: 8, children: [{ id: 11, name: "test1-1", age: 18, children: [{ id: 111, name: "test1-1-1", age: 28, children: [{ id: 1111, name: "test1-1-1-1", age: 38 }] }] }] }, { id: 2, name: "test2", age: 18, children: [{ id: 21, name: "test2-1", age: 28, children: [{ id: 211, name: "test2-1-1", age: 38, children: [{ id: 2111, name: "test2-1-1-1", age: 48 }] }] }] }, { id: 3, name: "test3", age: 28, children: [{ id: 31, name: "test3-1", age: 68 }] }, { id: 4, name: "test4", age: 38, children: [{ id: 41, name: "test4-1", age: 88 }] }, { id: 5, name: "test5", age: 48, children: [{ id: 51, name: "test5-1", age: 48 }] }, { id: 6, name: "test6", age: 58, children: [{ id: 61, name: "test6-1", age: 28 }] }] function arrayFn(data) { data.forEach(item => { if (item.age < 30 && item.age>10 ) { console.log(item) } item.children.forEach(bb => { arrayFn(bb) }) }) } arrayFn(data) </script> 重新上傳 vue中報錯了 Uncaught RangeError:超出最大調(diào)用堆棧大小 <script> export default { data() { return { number1: 0, number2: 0, data1: [], data: [ { id: 1, name: "test1", age: 8, children: [ { id: 11, name: "test1-1", age: 18, children: [ { id: 111, name: "test1-1-1", age: 28, children: [ { id: 1111, name: "test1-1-1-1", age: 38 } ] } ] } ] }, { id: 2, name: "test2", age: 18, children: [ { id: 21, name: "test2-1", age: 28, children: [ { id: 211, name: "test2-1-1", age: 38, children: [ { id: 2111, name: "test2-1-1-1", age: 48 } ] } ] } ] }, { id: 3, name: "test3", age: 28, children: [{ id: 31, name: "test3-1", age: 68 }] }, { id: 4, name: "test4", age: 38, children: [{ id: 41, name: "test4-1", age: 88 }] }, { id: 5, name: "test5", age: 48, children: [{ id: 51, name: "test5-1", age: 48 }] }, { id: 6, name: "test6", age: 58, children: [{ id: 61, name: "test6-1", age: 28 }] } ] }; }, methods: { btn() { if ( this.number1 >= this.number2 || this.number1 < 0 || this.number1 > 100 || this.number2 < 0 || this.number2 > 100 ) { alert("輸入錯誤"); return false; } else { this.filterFn() console.log(this.data1); } }, filterFn(){ var that = this; this.data.forEach(item => { if(item.children){ that.filterFn(item.children) } if(item.age < this.number2 && item.age >this.number1){ delete item.children; this.data1.push(item) } }) } } }; </script>
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經(jīng)驗 獲得超10個贊

arrayFn方法里面的bb不是數(shù)組,不能遞歸了

    function arrayFn(data) {
        data.forEach(item => {
            if (item.age < 30 && item.age>10 ) {
                console.log(item)
            }
            item.children.forEach(bb => {
                arrayFn(bb) // 這個bb不是數(shù)組
            })

        })
    }

你要判斷是否有children, 然后遞歸

    function arrayFn(data) {
        data.forEach(item => {
            if (item.age < 30 && item.age>10 ) {
                console.log(item)
            }
            if(item.children) {
                arrayFn(item.children)
            }
        })
    }

結(jié)果
https://img1.sycdn.imooc.com//5c8f42e90001464404440170.jpg

去掉children

    function arrayFn(data) {
        data.forEach(item => {
            if(item.children) {
                arrayFn(item.children)
            }
            if (item.age < 30 && item.age>10 ) {
                delete item.children
                console.log(item)
            }
            
        })
    }

結(jié)果

https://img1.sycdn.imooc.com//5c8f42ea000128e303070162.jpg

查看完整回答
反對 回復(fù) 2019-03-18
  • 1 回答
  • 0 關(guān)注
  • 1044 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號