0

I have some objects that I'm trying to merge them together in Node.js First object is in a format like this

{ ids : [id, id, id, ....]}

Rest of the object is in a format like this

{ postcode: '' , lat: '', lon: ''}, {postcode: '' , lat: '', lon: ''},....

I need to merger them in a way that looks like a format similar to this

{ id: '', postcode: '', lat: '', lon:''}

I was wondering would it be possible to do that in nodejs?

5
  • are the data related at the same index? do you want to get new objects with id and lat, lon without postcode? Commented Feb 20, 2018 at 19:00
  • @NinaScholz Yes they are related. so I want to add every id in the array to an object. for example id at first index of the array to the first object, id at second index of array to second object and so on. Eventually I will multiple objects or a single array that contains {id: , postcode:, lat:, lon:,}.... Commented Feb 20, 2018 at 19:12
  • @ScottSauyet I have tried looping through them and merge them but some reason, I either miss an object or the data never match. The problem is I am java developer and now working with node and its async nature has really confused me! Commented Feb 20, 2018 at 19:13
  • Out of curiosity, is this to merge a query of ids with an array of results from a database? If so, depending on the database, the objects can already contain the id if you tweak the query parameters. Commented Feb 20, 2018 at 19:23
  • The point was the you really should include some discussion in your question about what you've done so far, usually by showing some code. Commented Feb 20, 2018 at 20:55

4 Answers 4

2

ECMAScript 2015 (ES6) Standard Method

Object.assign(obj1, obj2);
Sign up to request clarification or add additional context in comments.

Comments

0

You could prepend the object with the 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);

Comments

0

Try this. Object.assign mutates your objects.

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]}));

Comments

0

Using function forEach.

Assuming the objects are both within the same array.

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; }

Separated objects

This approach modifies the source array.

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; }

Using function map.

Assuming the objects are both within the same array.

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; }

Separated objects

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; }

9 Comments

These are great answers, thank you! However, my array in your answer is not inside an array so its like {},{},{} and when i push it into an array then I get the result i want but it never increments. So, its the same id for all the objects! Any Idea how to increment it?
@M.Asgari can you share the input and output in your question to understand better what you're looking for?
So these are my inputs, { 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' } which is the first input then i have another input which is { ids: [ 5a6df69d84a1d08d9104c0b2, 5a6df69d84a1d08d9104c0b3, 5a6df69d84a1d08d9104c0b4]}
my output should be these two merged, like {id(each one unique) : ' ' , postcode: ' ' , lat: ' ', lon: ' ' }......
@M.Asgari the inputs are within an array?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.