其他分享
首页 > 其他分享> > 根据一个数组的值过滤出对应数据组的值

根据一个数组的值过滤出对应数据组的值

作者:互联网

        let refList = ['a', 'b', 'c', 'd', 'e'];
        let choose = ['b', 'd', 'e'];
        let res = refList.filter(
            el => {
              return choose.indexOf(el)>-1
            }
        )
        console.log('res', res)

res (3) ["b", "d", "e"]

更为复杂的运用

    let r1=['a','e'];
    let c1=[
        {studio:"CDU",
         group:[
             {
             name:'b'
            },
            {
             name:'d'
            },
            {
             name:'a'
            },
            ,
            {
             name:'c'
            },
            ,
            {
             name:'e'
            },
        ]
        }
    ]
    //c1为总数组,r1为用于过滤的数组
    let res1=c1.map(
        el=>{
           let aa = el.group.filter(
                 t=>{
                 return  r1.indexOf(t.name)>-1;
                 }
             )
             console.log('AA',aa)
             return aa
        }
    )
    console.log('res1',res1)

res1

  1. [Array(2)]
    1. 0: Array(2)
      1. 0: {name: "a"}
      2. 1: {name: "e"}
      3. length: 2
   

标签:aa,el,name,组的值,res,res1,过滤,let,数组
来源: https://www.cnblogs.com/Crying/p/16417481.html