0

My node.js app receives two json objects from external API. The first object is weather data by days where the keys are unix timestamp:

{
  1491368400: /*some data*/,
  1491454800: /*some data*/,
  1491541200: /*some data*/,
}

The second object is weather by hours for these days (if there are three days in object above, there are 3 * 24 keys in this object):

{
  1491368400: /*some data*/,
  1491372000: /*some data*/,
  1491375600: /*some data*/,
  .............................
  /* there are 72 keys, 24 for every proper day from object above */
}

I need to combine these two objects: take data from second object with hours and calculate average values for each day from first object and put these values into the first object. I think it would be good to do it with Transform Stream, but have no idea how to do it properly. I need just a schema how to do it, not detailed solution.

UPDATE

The main thing I want to know is how to merge two objects without putting much pressure on event loop.

8
  • Transform stream?? There are only "two" objects are there not? Where's the stream? Commented Nov 9, 2017 at 7:23
  • @NeilLunn If I am wrong about solution, what is the best tool in this case? Commented Nov 9, 2017 at 7:25
  • Just trying to clear up that you were simply telling us something fancy you googled but don't really know what it is. We can remove the "stream" tag from your question since it actually has nothing to do with streams then? Commented Nov 9, 2017 at 7:26
  • @NeilLunn these objects are received from API and maybe Strean is a good tool to merger these objects asyncrounosly? Commented Nov 9, 2017 at 7:30
  • @NeilLunn I want to merger them asyncrounosly, because there are prety much data in hourly object and I want to merge them hour by hour asyncrounosly. Commented Nov 9, 2017 at 7:32

3 Answers 3

1
Object.assign()

Can be used for this. It merges all argument objects to the first argument. If you don't want to modify your start objects, do:

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

Comments

0

If you can assume that you will always have 24 data points for every day, you could use Object.keys() to get an array of the timestamps for both the days and hours and order them. Then you can match the first day timestamp with the first 24 hour timestamps, second day with the second set of 24 hour timestamps, etc.

Due to your request for a direction and not code, I'll leave the rest up to you. If you need more though, please don't hesitate to ask and I'll edit the answer.

1 Comment

Sorry for not giving more details, but I am more interested in how I can merge two objects ayncrounosly without putting pressure on event loop. First I need to modify each object and then I need to merge them. Could you give an example how to do it properly? Maybe Stream Transform is good for that?
0

Combine objects

If you using es5, Object.assign({}, obj1, obj2);

or you using es6 or above standard, use spread operator to combine your objects

Ex const obj1 = { a: 1 }; const obj2 = { b: 2 }; const data = { ...obj1, ...obj2 };

// Now data value is { a: 1, b: 2 } 
// if keys are same , obj2 override obj1 value` 

Using triple dots(...) to spread your properties.

Comments

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.