繁体   English   中英

用三个键合并数组对象

[英]merge array object with three keys

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

例如

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

序列数组不一样。

我期待 C.

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

我认为减少和映射和过滤......但没有我想象的那么好。 我认为make函数...输入是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
    }
  },
]

这是一种方法:

它用 :

免责声明:由于它使用~运算符,如果您的容忍度不是 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))

这是vanilla 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 个对象数组:

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

为了得到你需要的东西,你可以在arr.map()方法中使用arr.find()并进行我们的更改:

 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM