在 JavaScript 中合并两个对象
Merging two objects in JavaScript
我有两个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"
};
我有什么办法将两个对象组合成一个具有两个子对象(关联数组)的对象?我想循环它并每次获得一组属性?
谢谢
是的,那些不是数组,但你可以有对象数组,我认为你正在寻找的是这个:
var attributes = [
{
"type": "text/css",
"rel": "stylesheet",
"href": baseUrl + "Styles/HtmlLibrary.css"
},
{
"type": "text/css",
"rel": "stylesheet",
"href": baseUrl + "Styles/jquery.contextMenu.css"
}];
是的,你可以很容易地做到这一点:-
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)
}
这是小提琴网址 http://jsfiddle.net/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 combined = {};
combined.attributes1 = attributes1;
combined.attributes2 = attributes2;
for(var attribute in combined){
if(combined.hasOwnProperty(attribute)){
console.log(attribute);
console.log(combined[attribute]);
}
}
相关文章:
- 正在更新mongod中两个对象内部的数组
- 添加两个对象以获取值的总和
- 如何使用javascript合并两个对象数组
- 在JavaScript中相等两个对象.在更改一时,秒会自动更改
- 如何使用AngularJs比较两个对象的JSON
- 如何在javascript中合并两个对象数组
- JavaScript - 合并两个对象数组并根据属性值删除重复数据
- Javascript如何确定两个对象是否相同
- 比较两个对象数组
- Javascript tests - 如何比较 Jasmine 中的两个对象
- JavaScript:为什么 array.push() 附加两个对象而不是一个对象
- 两个对象相等,但 js 确实说假
- 两个对象之间的Javascript原型
- 使用angularjs或javascript的两个对象数组的交集
- 合并两个对象而不覆盖
- 如何获取具有相同值的两个对象,并将其视为一个对象
- 比较两个对象数组中的重复对象,并在其'这在JavaScript中不是重复的
- 比较underscorejs中的两个对象数组
- 查找/合并具有共同属性的两个对象
- 有没有一个lodash函数可以合并两个对象并删除其中一个对象的属性(如果它们没有)'不存在于另一个中