1

2 つの JavaScript オブジェクトがあります。

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"       
};

2 つのオブジェクトを 2 つのサブオブジェクト (連想配列) を持つ 1 つのオブジェクトに結合する方法はありますか? 私はそれをループして、毎回1​​セットの属性を取得したいですか?

ありがとう

4

3 に答える 3

3

はい、それらは配列ではありませんが、オブジェクトの配列を持つことができます。探しているのはこれだと思います:

var attributes = [
                  {
                   "type": "text/css", 
                   "rel": "stylesheet",
                   "href": baseUrl + "Styles/HtmlLibrary.css"      
                  },
                  {
                   "type": "text/css",
                   "rel": "stylesheet",
                   "href": baseUrl + "Styles/jquery.contextMenu.css"       
                  }];
于 2012-07-04T06:14:51.623 に答える
3

ええ、あなたは簡単にそれを行うことができます:-

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)

        }

これはフィドルの URL http://jsfiddle.net/HSdJh/1/です。

于 2012-07-04T06:14:52.713 に答える
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]);
    }

}
于 2012-07-04T06:28:03.330 に答える