associative array - Merging two objects in JavaScript - Stack Overflow

I have two JavaScript objects:var attributes1 = {"type": "textcss","rel"

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 bine 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

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 bine 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

Share Improve this question edited Jul 4, 2012 at 6:10 Ray Toal 88.5k20 gold badges184 silver badges243 bronze badges asked Jul 4, 2012 at 6:06 OviOvi 2,5599 gold badges52 silver badges72 bronze badges 4
  • 1 There is no concept of associative arrays in JS, these are js objects – Blaster Commented Jul 4, 2012 at 6:07
  • 1 You mean like var attributes = [{...},{...}];? I remend to read the MDN JavaScript Guide, it should give you a good understanding of the JS basics. – Felix Kling Commented Jul 4, 2012 at 6:08
  • 1 duplicate: stackoverflow./questions/171251/… – Blaster Commented Jul 4, 2012 at 6:08
  • I think @FelixKling has it right here. It is not exactly a merge, the OP seems to be asking for a way to bine both objects into 1, which is trivial like this: [attributes1, attributes2] or even ({library: attributes1, menu: attributes2}) – Ray Toal Commented Jul 4, 2012 at 6:19
Add a ment  | 

3 Answers 3

Reset to default 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"       
                  }];

Yeah you can easily do that:-

var baseUrl="www.google." // 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/HSdJh/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 bined = {};

bined.attributes1 = attributes1;
bined.attributes2 = attributes2;


for(var attribute in bined){

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

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745297692a4621241.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信