Add an object into another object

0 Marin BeŇ°lo · August 21, 2015
Hello! :D

If I have an object "obj1", and it contains 2 other objects inside... 
How can I add a new object to my "obj1"?

var obj1 = {
     aa:{
 
     },
     bb:{

     },
     HOW CAN I ADD HERE A NEW OBJECT? :D
};

Post a Reply

Replies

Oldest  Newest  Rating
0 Brotsky Tv · August 24, 2015
hey there!

favorite
2
Given
options = {
underscored: true
}

products = {
foo: bar
}
I'd like to get
products = {
underscored: true
foo: bar
}


here is the solution :

down vote accepted
<script>
function mix(source, target) {
  for(var key in source) {
    if (source.hasOwnProperty(key)) {
       target[key] = source[key];
    }
  }

}

 mix(options, products);
</script>


For more Javascript tutorials:  https://www.brotsky.tv/category/javascript/
  • 1

Javascript

107,119 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators