簡體   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