用三个键合并数组对象

时间:2019-09-04 13:50:15

标签: javascript arrays object merge

有两个对象数组,a和b。密钥是“ id”,“ isfix”,“ groupid”。

例如

a.id === b.id && a.isfix === b.isfix &&  a.groupid===b.groupdid

序列数组不同。

我应该是c。

我希望您不要使用lodash。我喜欢es6或vanila js。谢谢。

我认为reduce和map以及filter ..但是不如我所想。 我认为使功能... 输入是a,b,输出是c

var a = [
  {
    id:"555",
    groupID:"10",
    isFix:false,
    tolerancePlus:5,
    toleranceMinus:3
  },
  {
    id:"123",
    groupID:"10",
    isFix:true,
    tolerancePlus:"",
    toleranceMinus:7
  },
  {
    id:"555",
    groupID:"10",
    isFix:true,
    tolerancePlus:11,
    toleranceMinus:6
  }
]

var b =  [
  {
    id:"123",
    groupID:"10",
    isFix:true,
    tolerance:{
      min: null,
      plus : null
    }
  },
  {
    id:"555",
    groupID:"10",
    isFix:false,
    tolerance:{
      min: null,
      plus : null
    }
  },
  {
    id:"555",
    groupID:"10",
    isFix:true,
    tolerance:{
      min: null,
      plus : null
    }
  },
]

var c =  [
  {
    id:"123",
    groupID:"10",
    isFix:true,
    tolerance:{
      min: 7,
      plus : 0 // if "" that value is 0
    }
  },
  {
    id:"555",
    groupID:"10",
    isFix:false,
    tolerance:{
      min: 3,
      plus : 5
    }
  },
  {
    id:"555",
    groupID:"10",
    isFix:true,
    tolerance:{
      min: 6,
      plus : 11
    }
  },
]

3 个答案:

答案 0 :(得分:0)

这是一种实现方法:

它使用:

免责声明:由于它使用~运算符,因此如果您的容差不是32位整数(它是未定义的行为AFAIR),它将(并且将)中断

// create your arrays
var a = [{id:"555",groupID:"10",isFix:false,tolerancePlus:5,toleranceMinus:3},{id:"123",groupID:"10",isFix:true,tolerancePlus:"",toleranceMinus:7},{id:"555",groupID:"10",isFix:true,tolerancePlus:11,toleranceMinus:6}]

var b =  [{id:"123",groupID:"10",isFix:true,tolerance:{min: null,plus : null}},{id:"555",groupID:"10",isFix:false,tolerance:{min: null,plus : null}},{id:"555",groupID:"10",isFix:true,tolerance:{min: null,plus : null}},]

// loop over b, creating a new array
let c = b.reduce((acc, value) => {
  
// find an object from a which correspond to the current object
  let linked = a.find(val => val.id === value.id && value.groupID === val.groupID && val.isFix === value.isFix)
  
// if it exists push it in the new array
  if(linked) {
    acc.push({
      id: linked.id,
      groupID: linked.groupID,
      isFix: linked.isFix,
      tolerance:{
        min: ~~linked.toleranceMinus, // the ~~value here use some magic to transform
        plus : ~~linked.tolerancePlus // everything that's not a number to 0
      }
    })
  }
  return acc
  
}, [])

console.log(c)

var wantedC =  [{id:"123",groupID:"10",isFix:true,tolerance:{min: 7,plus : 0}},{id:"555",groupID:"10",isFix:false,tolerance:{min: 3,plus : 5}},{id:"555",groupID:"10",isFix:true,tolerance:{min: 6,plus : 11}}]

console.log(JSON.stringify(wantedC) === JSON.stringify(c))

答案 1 :(得分:0)

这是香草JS中的逻辑:

var a = [
  {
    id: '555',
    groupID: '10',
    isFix: false,
    tolerancePlus: 5,
    toleranceMinus: 3
  },
  {
    id: '123',
    groupID: '10',
    isFix: true,
    tolerancePlus: '',
    toleranceMinus: 7
  },
  {
    id: '555',
    groupID: '10',
    isFix: true,
    tolerancePlus: 11,
    toleranceMinus: 6
  }
]
var b = [
  {
    id: '123',
    groupID: '10',
    isFix: true,
    tolerance: {
      min: null,
      plus: null
    }
  },
  {
    id: '555',
    groupID: '10',
    isFix: false,
    tolerance: {
      min: null,
      plus: null
    }
  },
  {
    id: '555',
    groupID: '10',
    isFix: true,
    tolerance: {
      min: null,
      plus: null
    }
  }
]

var c = a.map(data1 => {
  const toleranceData = b.map(data2 => {
    if (
      data1.id === data2.id &&
      data1.isfix === data2.isfix &&
      data1.groupdid === data2.groupdid
    ) {
      return {
        tolerance: {
          min: data1.toleranceMinus || 0,
          plus: data1.tolerancePlus || 0
        }
      }
    }
  })
  const { tolerance } = toleranceData.filter(d => d)[0]
  const { id, groupID, isFix } = data1
  return { id, groupID, isFix, tolerance }
})

console.log(c)

答案 2 :(得分:0)

所以我们有2个对象数组:

我们有一个声明a.id === b.id && a.isFix === b.isFix && a.groupid===b.groupdid

要获得所需的信息,可以在arr.find()方法内使用arr.map()并进行更改:

const a = [{ id: "555", groupID: "10", isFix: false, tolerancePlus: 5, toleranceMinus: 3 }, { id: "123", groupID: "10", isFix: true, tolerancePlus: "", toleranceMinus: 7 }, { id: "555", groupID: "10", isFix: true, tolerancePlus: 11, toleranceMinus: 6 } ] 
const b = [{ id: "123", groupID: "10", isFix: true, tolerance: { min: null, plus: null } }, { id: "555", groupID: "10", isFix: false, tolerance: { min: null, plus: null } }, { id: "555", groupID: "10", isFix: true, tolerance: { min: null, plus: null } }, ]

let c = b.map(obj => {
  const valuesObj = a.find(item => item.id === obj.id && item.isFix === obj.isFix && item.groupid === obj.groupdid);
  if (valuesObj) {
    obj.tolerance.min = valuesObj.toleranceMinus || 0;
    obj.tolerance.plus = valuesObj.tolerancePlus || 0;
  }
  return obj;
})
console.log(c);