[英]Merge/join two objects based on a common key (push? splice?)
我一直在嘗試使用push
和splice
等,但沒有成功。 我希望您能給我一個(或兩個)線索以使其起作用。 在我的問題下面,這似乎並不難... :(
我有2個對象:
對象#1:
houses: [ { _id: 1,
KLLS: '72797-194155',
date : '01/01/1984'},
{ _id: 2,
KLLS: '84773-949399',
date : '01/01/1984'}
]
對象2:
works: [ { _id: 27,
KLLS: '72797-194155',
stuff : 'some stuff'},
{ _id: 28,
KLLS: '72797-194155', // Note that KLLS key is the same as id:27
stuff : 'some stuff'},
{ _id: 29,
KLLS: '84773-949399',
stuff : 'some stuff'},
{ _id: 30,
KLLS: '84773-949399', // Note that KLLS key is the same as id:29
stuff : 'some stuff'},
]
我想要實現的是這樣的:
[ { _id: 1,
KLLS: '72797-194155',
date : '01/01/1984',
stuff:
[ { _id: 27,
KLLS: '72797-194155',
stuff : 'some stuff'},
{ _id: 28,
KLLS: '72797-194155',
stuff : 'some stuff'}
]
},
{ _id: 2,
KLLS: '84773-949399',
date : '01/01/1984',
stuff:
[ { _id: 27,
KLLS: '72797-194155',
stuff : 'some stuff'},
{ _id: 28,
KLLS: '72797-194155',
stuff : 'some stuff'}
]}
]
其實我在這一點上(使用Lodash):
for(var i = 0; i < houses.length; i++) {
// get the KLLS key for each house[i]
var klls = houses[i].KLLS
// retrieve the works corresponding to KLLS key for house[i]
var trvx = _.filter(works, ['KLLS', klls])
// Merge the two to get the wanted output...
--> Whatever I try here, nothing works...
even using splice or push...
}
你有什么線索讓我步入正軌嗎?
你可以這樣做...
var a = [{ _id: 1, KLLS: '72797-194155', date: '01/01/1984' }, { _id: 2, KLLS: '84773-949399', date: '01/01/1984' } ] var b = [{ _id: 27, KLLS: '72797-194155', stuff: 'some stuff' }, { _id: 28, KLLS: '72797-194155', // Note that KLLS key is the same as id:27 stuff: 'some stuff' }, { _id: 29, KLLS: '84773-949399', stuff: 'some stuff' }, { _id: 30, KLLS: '84773-949399', // Note that KLLS key is the same as id:29 stuff: 'some stuff' }, ] var newobj = [] var n = a.map(function(el) { var temp = [] b.map(function(el2) { if (el.KLLS === el2.KLLS) { temp.push(el2) } }) el.stuffs = temp newobj.push(el) }) console.log(newobj)
遺憾的是,有破折號而不使用任何方便的方法來解決。
作為houses
的JSON標記為“住宅”和works
標JSON的“作品”,你可以做如下,讓您的解決方案:
var res = _.reduce(houses, function (memo, house) {
return memo.concat(_.extend({}, house, {
stuff: _.filter(works, {KLLS :house.KLLS})
}));
}, []);
console.log(res);
想法是將houses
陣列reduce
為新的房屋陣列,並對其進行extended
。
我更喜歡創建一個新對象,而不是修改原始對象,以便使函數盡可能pure
。
合並數組,按KLLS將其分組,然后減少每個組數組以產生所需的對象:
function combine(arr1, arr2) { return _(arr1) .concat(arr2) // concat the 2nd array .groupBy('KLLS') // group the items by KLLS .map(function(group) { return group.reduce(function(result, obj) { if (obj.date) { // if it's an object with date, merge it with current result and return return _.merge(result, obj); } result.stuff.push(obj); return result; }, { stuff: [] }); // initial value is an object with stuff array }) .value(); // end the chain } var houses = [{ _id: 1, KLLS: '72797-194155', date: '01/01/1984' }, { _id: 2, KLLS: '84773-949399', date: '01/01/1984' }]; var works = [{ _id: 27, KLLS: '72797-194155', stuff: 'some stuff' }, { _id: 28, KLLS: '72797-194155', stuff: 'some stuff' }, { _id: 29, KLLS: '84773-949399', stuff: 'some stuff' }, { _id: 30, KLLS: '84773-949399', stuff: 'some stuff' }, ]; var result = combine(houses, works); console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.2/lodash.min.js"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.