Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining JavaScript Objects into One

I have a function called "Colorbox" (jQuery plugin) that takes a number of parameters like so:

$(this).colorbox({     width : "500px",     height : "500px" }); 

I have several different types of "this", though, each with their own properties. Like so:

var Type = {   video: {     width : "500px",     height : "500px"   },   gallery: {     width : "1065px",     height : "600px"   } } 

Beyond that, I have other behaviors, logic, and a 'default' group of settings (which get overwritten by more specific ones). What I'm trying to do is push all the appropriate settings, from multiple objects, into a single Object so I can just call:

$(this).colorbox(Settings); 

How would I transfer an unknown group of properties and their values (for example "width" and "height") from something like Type.video into Settings? The goal is to be able to call Settings.height and get back the value I pushed in.

like image 626
Doug Avery Avatar asked Aug 26 '09 13:08

Doug Avery


People also ask

How do I combine multiple objects into one JavaScript?

JavaScript Merge Objects To merge objects into a new one that has all properties of the merged objects, you have two options: Use a spread operator ( ... ) Use the Object. assign() method.

How do I merge an array of objects into one?

Use the Array. We can use the JavaScript array reduce method to combine objects in an array into one object. We have the arr array which we want to combine into one object. To do that, we call reduce with a callback that returns an object with obj spread into the returned object. And we add the item.

How do I merge two objects in TypeScript?

Use the spread syntax (...) to merge objects in TypeScript, e.g. const obj3 = { ... obj1, ... obj2 } . The type of the final object will successfully be inferred, so trying to add or remove properties from it will cause the type checker to show an error.

What is merge in JavaScript?

In JavaScript, an object is defined as a collection of key-value pairs. An object is also a non-primitive data type. You'll oftentimes need to combine objects into a single one which contains all the individual properties of its constituent parts. This operation is called merging.


2 Answers

Take a look at the JQuery extend method. It can merge two objects together and all their properties.

From JQuery's example page:

var settings = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; jQuery.extend(settings, options); 

Now settings contains the merged settings and options objects.

like image 188
Matthew Manela Avatar answered Sep 21 '22 17:09

Matthew Manela


JavaScript have a simple native function to merge object. which is Object.assign() introduced in ES6.

  // creating two JavaScript objects     var x = { a: true };var y = { b: false}; // merging two objects with JavaScript native function     var obj = Object.assign(x,y);     //result     Console.log(obj); // output is { a: true, b: false } 

for more information about javascript merging object please check at merge JavaScript objects with examples.

like image 41
Venu immadi Avatar answered Sep 21 '22 17:09

Venu immadi