JavaScript Using spread syntax to dynamically merge objects

The new spread syntax can be used to elegantly merge several objects in the following way:

const person = { name: 'David Walsh', gender: 'Male' };
const tools = { computer: 'Mac', editor: 'Atom' };

const summary = {...person, ...tools};

Is there a way to use this in a dynamical scenario, where the number and names of the objects to be merged are not known beforehand?

If

var objects_to_be_merged = get_objects();

generated an iterable list of objects during runtime, how can I use the spread operator iteratively to create a summary object like in the example above?

Answer:1

You can use Object.assign() with spread syntax to merge array of objects.

const person = { name: 'David Walsh', gender: 'Male' };
const tools = { computer: 'Mac', editor: 'Atom' };

const objects = () => [person, tools];
const summary = Object.assign({}, ...objects());
console.log(summary)
Answer:2

I want to use the selector in document.querySelector() to select all a tags with the text "print". <a href="https://www.google.co.in">print</a> how can I select the same. I don't want to ...

I want to use the selector in document.querySelector() to select all a tags with the text "print". <a href="https://www.google.co.in">print</a> how can I select the same. I don't want to ...

  1. select anchor tags html
  2. select anchor tags using jquery
  3. select anchor tag in jquery
  4. select anchor tag with class jquery
  5. select anchor tag with text jquery
  6. select anchor tag with id in jquery
  7. select anchor tag javascript
  8. select anchor tag in selenium
  9. select anchor tag
  10. select anchor tag with text javascript
  11. select anchor tag xpath
  12. select all anchor tags in jquery
  13. select all anchor tags javascript
  14. select all anchor tags
  15. jquery select anchor tag with href
  16. select option anchor tag
  17. jquery select anchor tag within div
  18. select last anchor tag
  19. select dropdown anchor tag
  20. xpath select all anchor tags

I have a table and i created own scrollbar for iPad in my project. Now, the table is moving as the user touch and drag it, but how to move my scrollbar according to that table. I tried but for me ...

I have a table and i created own scrollbar for iPad in my project. Now, the table is moving as the user touch and drag it, but how to move my scrollbar according to that table. I tried but for me ...

I would like to see this column, I can see it here https://developers.google.com/web/fundamentals/performance/resource-prioritization But I can't see it inside my Chrome Dev tools's Network tab. My ...

I would like to see this column, I can see it here https://developers.google.com/web/fundamentals/performance/resource-prioritization But I can't see it inside my Chrome Dev tools's Network tab. My ...

I have a list of airports in XML format which I would like to use with jquery autocomplete. The XML file looks like this: <item code="AAR" airport="Aarhus" country="Denmark" /> When a ...

I have a list of airports in XML format which I would like to use with jquery autocomplete. The XML file looks like this: <item code="AAR" airport="Aarhus" country="Denmark" /> When a ...

  1. jquery autocomplete from database c#
  2. jquery autocomplete from database
  3. jquery autocomplete from database javascript
  4. jquery autocomplete from json
  5. jquery autocomplete from list
  6. jquery autocomplete from array
  7. jquery autocomplete from database php
  8. jquery autocomplete from json file
  9. jquery autocomplete from database java
  10. jquery autocomplete from api
  11. jquery autocomplete from database spring mvc example
  12. jquery autocomplete from database jsp
  13. jquery autocomplete from database c# mvc
  14. jquery autocomplete from ajax
  15. jquery autocomplete from database asp.net
  16. jquery autocomplete from sharepoint list
  17. jquery autocomplete from database laravel
  18. jquery autocomplete from database mvc
  19. jquery autocomplete from json array
  20. jquery autocomplete from mysql