我有一些对象,我试图在Node.js中将它们合并在一起 第一个对象的格式如下
{ ids : [id, id, id, ....]}
对象的其余部分采用如下格式
{ postcode: '' , lat: '', lon: ''}, {postcode: '' , lat: '', lon: ''},....
我需要以类似于此
的格式合并它们{ id: '', postcode: '', lat: '', lon:''}
我想知道是否可以在nodejs中做到这一点?
答案 0 :(得分:2)
Object.assign(obj1, obj2);
答案 1 :(得分:0)
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; }

答案 2 :(得分: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]}));
答案 3 :(得分:0)
您可以使用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);