[英]How can I merge two objects of different lengths based on the values
我有兩個對象數組,兩個數組都包含鍵,值對。 我的目標是,如果一個鍵name
的字符串值與另一個鍵_id
的字符串值匹配,則將鍵值從一個數組分配給第二個數組。
我已經嘗試將兩個數組組合成一個數組,並且只有一個對象數組,但是我仍然不確定如何根據鍵_id
和name
匹配值將鍵和值分配給其他對象。 我知道如何使用相同的鍵,但是我們如何基於不同的鍵和相同的值呢? 這可能是不好的設計和實踐,但這就是數據返回的方式。 可以嗎 我嘗試使用lodash,傳播操作,地圖均未成功。
arr1 = [
{"_id": "electronics", "number": 35 },
{"_id": "house Appliances", "number": 5 },
{"_id": "nothing", "number":0}
]
arr2 = [
{"name": "electronics", "purpose": "entertainment", "price": 100},
{"name": "house Appliances", "purpose": "general", "price": 200},
{"name": "watches", "purpose": "time", "price": 30} ]
combinedArrObj = [
{"name": "electronics", "number": 35, "purpose": "entertainment", "price": 100},
{"name": "house Appliances", "purpose": "general", "price": 200, "number": 5},
{"name": "watches", "purpose": "time", "price": 30}
]
在我看來,如果只在array1
有一對key value
對,則不需要太復雜。 只需轉換為地圖,然后通過array2
和Object.assign即可轉換為Array.map ,以創建不帶lodash等的合並。
const arr1 = [{"_id": "electronics", "number": 35 }, {"_id": "house Appliances", "number": 5 }] const arr2 = [{"name": "electronics", "purpose": "entertainment", "price": 100},{"name": "house Appliances", "purpose": "general", "price": 200},{"name": "watches", "purpose": "time", "price": 30}] let map = arr1.reduce((acc, {_id, number}) => (acc[_id] = number, acc), {}) let result = arr2.map(({name, ...rest}) => Object.assign({ name }, rest, map[name] ? { number: map[name] } : {})) console.log(result)
您可以使用reduce和find
arr1
是否存在對應名稱的匹配_id
inp
保留在輸出中 let arr1 = [{"_id": "electronics", "number": 35 },"_id": "house Appliances", "number": 5 }] let arr2 = [{"name": "electronics", "purpose": "entertainment", "price": 100},{"name": "house Appliances", "purpose": "general", "price": 200},{"name": "watches", "purpose": "time", "price": 30} ] let final = arr2.reduce((op,inp)=>{ let found = arr1.find(({_id})=>_id===inp.name) let name = inp.name op[name] = op[name] || {} if(found){ let {_id, ...rest} = found op[name] = {...op[name], ...rest, ...inp} } else { op[name] = {...op[name], ...inp} } return op },{}) console.log(final)
使用_.flow()
創建一個函數,該函數使用_.overArgs()
將每個數組轉換為對象,並使用其各自的標識符( _id
或name
)作為鍵(使用_.keyBy()
), 並將它們合並到單個對象中。 通過省略 _id
字段的映射將對象轉換回數組:
const { flow, partialRight: pr, overArgs, merge, keyBy, map, omit } = _ const combineArrays = flow( overArgs((...objs) => merge({}, ...objs), [ arr1 => keyBy(arr1, '_id'), arr2 => keyBy(arr2, 'name') ]), pr(map, o => omit(o, '_id')) ) const arr1 = [{"_id": "electronics", "number": 35 }, {"_id": "house Appliances", "number": 5 }] const arr2 = [{"name": "electronics", "purpose": "entertainment", "price": 100},{"name": "house Appliances", "purpose": "general", "price": 200},{"name": "watches", "purpose": "time", "price": 30}] const combinedArrs = combineArrays(arr1, arr2) console.log(combinedArrs)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
還有一個使用lodash / fp的簡短版本:
const { flow, overArgs, merge, keyBy, map, omit } = _ const combineArrays = flow( overArgs(merge, [keyBy('_id'), keyBy('name')]), map(omit('_id')) ) const arr1 = [{"_id": "electronics", "number": 35 }, {"_id": "house Appliances", "number": 5 }] const arr2 = [{"name": "electronics", "purpose": "entertainment", "price": 100},{"name": "house Appliances", "purpose": "general", "price": 200},{"name": "watches", "purpose": "time", "price": 30}] const combinedArrs = combineArrays(arr1, arr2) console.log(combinedArrs)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
使用Vanilla JS,您可以合並數組,然后使用Array.reduce()
以_id
或name
作為鍵將數組轉換為對象,並將具有相同鍵的項與對象spread結合使用。 然后使用Object.values()
轉換回數組:
const combineArrays = (arr1, arr2) => Object.values( arr1.concat(arr2) // combine the arrays .reduce((r, { _id, ...o }) => { // clone the object and remove _id if exists const key = _id || o.name // the key is the _id or the name if _id doesn't exist r[key] = r[key] ? { ...r[key], ...o } : o // if the key exists on the accumulator combine with current item, if not set current item to the key return r; }, {}) ) const arr1 = [{"_id": "electronics", "number": 35 }, {"_id": "house Appliances", "number": 5 }] const arr2 = [{"name": "electronics", "purpose": "entertainment", "price": 100},{"name": "house Appliances", "purpose": "general", "price": 200},{"name": "watches", "purpose": "time", "price": 30}] const combinedArrs = combineArrays(arr1, arr2) console.log(combinedArrs)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.