[英]Merging objects in node.js
我有一些對象試圖在Node.js中將它們合並在一起,第一個對象的格式如下
{ ids : [id, id, id, ....]}
其余對象的格式如下
{ postcode: '' , lat: '', lon: ''}, {postcode: '' , lat: '', lon: ''},....
我需要以類似於此格式的方式合並它們
{ id: '', postcode: '', lat: '', lon:''}
我想知道是否可以在nodejs中做到這一點?
Object.assign(obj1, obj2);
forEach
。 假設對象都在同一數組中。
var array = [{ ids : ['5a6df69d84a1d08d9104c0b2', '5a6df69d84a1d08d9104c0b3', '5a6df69d84a1d08d9104c0b4']}, { postcode: 'BL9 9LS', lat: 53.5784531917354, lon: -2.30434868940289, admin_ward: 'E05000682' }, { postcode: 'SK3 0RH', lat: 53.4003081612165, lon: -2.19362957330647, admin_ward: 'E05000788' }, { postcode: 'BS1 5BQ', lat: 51.4564431573373, lon: -2.59829088044349, admin_ward: 'E05010892' }]; var result = array.slice(-(array.length - 1)); // Create a new Array removing the first position. result.forEach((e, i) => e['id'] = array[0].ids[i] /* Add id from the first element*/); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
分開的物體
此方法修改了源數組。
var firstObject = { ids : ['5a6df69d84a1d08d9104c0b2', '5a6df69d84a1d08d9104c0b3', '5a6df69d84a1d08d9104c0b4']}; var array = [{ postcode: 'BL9 9LS', lat: 53.5784531917354, lon: -2.30434868940289, admin_ward: 'E05000682' }, { postcode: 'SK3 0RH', lat: 53.4003081612165, lon: -2.19362957330647, admin_ward: 'E05000788' }, { postcode: 'BS1 5BQ', lat: 51.4564431573373, lon: -2.59829088044349, admin_ward: 'E05010892' }]; array.forEach((e, i) => e['id'] = firstObject.ids[i]); console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
map
。 假設對象都在同一數組中。
var array = [{ ids : ['5a6df69d84a1d08d9104c0b2', '5a6df69d84a1d08d9104c0b3', '5a6df69d84a1d08d9104c0b4']}, { postcode: 'BL9 9LS', lat: 53.5784531917354, lon: -2.30434868940289, admin_ward: 'E05000682' }, { postcode: 'SK3 0RH', lat: 53.4003081612165, lon: -2.19362957330647, admin_ward: 'E05000788' }, { postcode: 'BS1 5BQ', lat: 51.4564431573373, lon: -2.59829088044349, admin_ward: 'E05010892' }]; var result = array.slice(-(array.length - 1)); // Create a new Array removing the first position. result = result.map((e, i) => ({ ...e, ...{'id': array[0].ids[i] } }) /* Add id from the first element*/); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
分開的物體
var firstObject = { ids : ['5a6df69d84a1d08d9104c0b2', '5a6df69d84a1d08d9104c0b3', '5a6df69d84a1d08d9104c0b4']}; var array = [{ postcode: 'BL9 9LS', lat: 53.5784531917354, lon: -2.30434868940289, admin_ward: 'E05000682' }, { postcode: 'SK3 0RH', lat: 53.4003081612165, lon: -2.19362957330647, admin_ward: 'E05000788' }, { postcode: 'BS1 5BQ', lat: 51.4564431573373, lon: -2.59829088044349, admin_ward: 'E05010892' }]; var result = array.map((e, i) => ({ ...e, ...{ 'id': firstObject.ids[i] } })); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
嘗試這個。 Object.assign會改變您的對象。
let idsObj = { ids : [id, id, id, ....]};
let postCodeArray = [{ postcode: '' , lat: '', lon: ''}, {postcode: '' , lat: '', lon: ''}];
postCodeArray.forEach((elem,index) => Object.assign(elem,{id: idsObj.ids[index]}));
您可以在對象前面加上id
。
var object1 = { ids: [23, 42] }, postcodes = [{ postcode: 'XY' , lat: '11', lon: '22' }, { postcode: 'ZY' , lat: '33', lon: '44'}], merged = postcodes.map((o, i) => Object.assign({ id: object1.ids[i] }, o)); console.log(merged);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.