最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

associative array - Merging two objects in JavaScript - Stack Overflow

programmeradmin2浏览0评论

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]);
    }

}
发布评论

评论列表(0)

  1. 暂无评论