数组对比 (烦了好久)

<!DOCTYPE html >
< html >

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

     < body >
         < script >
             var arr1 = [ // 测试数据
                {
                     name: '红烧鱼',
                     size: '大',
                     taste: '微辣',
                     price: '40',
                     remain: '100'
                },
                {
                     name: '麻辣小龙虾',
                     size: '大',
                     taste: '麻辣',
                     price: '138',
                     remain: '200'
                },
                {
                     name: '清蒸小龙虾',
                     size: '大',
                     taste: '清淡',
                     price: '138',
                     remain: '200'
                }
            ]

             var arr2 = [ // 测试数据
                {
                     name: '红烧鱼1',
                     size: '大',
                     taste: '微辣',
                     price: '40',
                     remain: '100'
                },
                {
                     name: '麻辣小龙虾',
                     size: '大',
                     taste: '麻辣',
                     price: '138',
                     remain: '200'
                },
                {
                     name: '清蒸小龙虾1',
                     size: '大',
                     taste: '清淡',
                     price: '138',
                     remain: '200'
                }
            ]

             // let arr = arr2.filter(function(v){ return !(arr1.indexOf(v) > -1) })

             let arr = arr2. reduce(( total, next) => {
                 console. log( total)
                 console. log( next)
                 if( arr1. some( item => item. name === next. name)) {
                     console. log( "[...total]",[... total])
                     return [... total]
                } else {
                     console. log( "[...total, next]",[... total, next])
                     return [... total, next]
                }
            }, []);
             console. log( arr)

         < / script >
     </ body >

</ html >

猜你喜欢

转载自blog.csdn.net/weixin_38641550/article/details/80778942