数组的交叉相乘

1、两个数组实现交叉相乘

data(){
      return{
        arr1: [
         { a: 1 }, { b: 1 }
        ],  
        arr2: [
          { c: 2 }, { d: 2 }
        ]
  }
},
created(){
   this.mergeArry(this.arr1, this.arr2)
},
methods:{
  mergeArry(v1, v2) {
      var arr = [v1, v2]
      const newArry = arr.reduce((a, b) => a.map(x => b.map(y => x.concat(y))).reduce((a, b) => a.concat(b), []), [[]]).map(item => item.reduce((a, b) => { return { ...a, ...b } }, {}))
      console.log(1, newArry)
    },
}

2、单数组实现交叉相乘

data(){
      return{
        arr3: [
        [1, 2],
        ['a', 'b'],
        ['一', '二']
      ],
      w: []
  }
},
created(){
   this.w = this.mergeArry2(this.arr3)
    console.log(this.w)
},
methods:{
   mergeArry2(v3) {
      return v3.reduce((a, b) => a.map(x => b.map(y => x.concat(y))).reduce((a, b) => a.concat(b), []), [[]])
    },
}

 

posted @ 2020-10-10 16:34  mark224  阅读(563)  评论(0编辑  收藏  举报