1

I have two JavaScript objects:

var attributes1 = {
    "type": "text/css",
    "rel": "stylesheet",
    "href": baseUrl + "Styles/HtmlLibrary.css"      
};
var attributes2 = {
    "type": "text/css",
    "rel": "stylesheet",
    "href": baseUrl + "Styles/jquery.contextMenu.css"       
};

I there any way to combine the two objects into one object that has two subobjects (associative arrays)? I want to loop throgh it and get each time one set of attributes?

Thanks

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
Ovi
  • 2,459
  • 9
  • 50
  • 72
  • 1
    There is no concept of associative arrays in JS, these are js objects – Blaster Jul 04 '12 at 06:07
  • 1
    You mean like `var attributes = [{...},{...}];`? I recommend to read the [MDN JavaScript Guide](https://developer.mozilla.org/en/JavaScript/Guide), it should give you a good understanding of the JS basics. – Felix Kling Jul 04 '12 at 06:08
  • 1
    duplicate: http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically – Blaster Jul 04 '12 at 06:08
  • I think @FelixKling has it right here. It is not exactly a merge, the OP seems to be asking for a way to combine both objects into 1, which is trivial like this: `[attributes1, attributes2]` or even `({library: attributes1, menu: attributes2})` – Ray Toal Jul 04 '12 at 06:19

3 Answers3

3

yea those aren't arrays, but you can have arrays of objects, i think what you are looking for is this:

var attributes = [
                  {
                   "type": "text/css", 
                   "rel": "stylesheet",
                   "href": baseUrl + "Styles/HtmlLibrary.css"      
                  },
                  {
                   "type": "text/css",
                   "rel": "stylesheet",
                   "href": baseUrl + "Styles/jquery.contextMenu.css"       
                  }];
dano
  • 1,043
  • 1
  • 6
  • 11
3

Yeah you can easily do that:-

var baseUrl="www.google.com" // Base URL just for the sake of program.
var attributes = {
    "type": "text/css",
    "rel": "stylesheet",
    "href": baseUrl + "Styles/HtmlLibrary.css"      
};
var attributes1 = {
    "type": "text/css",
    "rel": "stylesheet",
    "href": baseUrl + "Styles/jquery.contextMenu.css"       
};
var k=[];
k.push(attributes);
k.push(attributes1)

    //Since K has both the associative arrays now you can refer using
    for(var i=0;i<k.length;i++)
        {
        console.log(k[i].type+" "+k[i].rel+" "+k[i].href)

        }

This is the fiddle URL http://jsfiddle.net/HSdJh/1/

Shiv Kumar Ganesh
  • 3,799
  • 10
  • 46
  • 85
1
var attributes1 = {
    "type": "text/css",
    "rel": "stylesheet",
    "href": baseUrl + "Styles/HtmlLibrary.css"      
};
var attributes2 = {
    "type": "text/css",
    "rel": "stylesheet",
    "href": baseUrl + "Styles/jquery.contextMenu.css"       
};

var combined = {};

combined.attributes1 = attributes1;
combined.attributes2 = attributes2;


for(var attribute in combined){

    if(combined.hasOwnProperty(attribute)){
        console.log(attribute);
        console.log(combined[attribute]);
    }

}