admin管理员组文章数量:1025251
I've got a big JavaScript object literal I am reusing with very few modifications each time. It would be good to make a variable containing most of the object, then reference that each time I need it.
But how can I do that? I tried to do this, but it didn't work:
var settings = {
legend: {
enabled:false
},
yAxis: {
title: {
text: 'Impact'
}
},
tooltip: {
formatter: function() {
return 'Impact: '+ this.y +' points'
}
},
credits: {
enabled: false
},
exporting: {
enabled: false
}
};
jQuery(document).ready(function() {
new Highcharts.Chart({
chart: {
renderTo: 'chart',
defaultSeriesType: 'column',
width: 450
},
title: {
text: 'Comparison of Impact for Male age 23'
},
xAxis: {
categories: [
'Good brain',
'Good humor',
'Good will'
],
},
series: [{
data: [5,2,8]
}],
settings // <-- HERE
});
});
I've got a big JavaScript object literal I am reusing with very few modifications each time. It would be good to make a variable containing most of the object, then reference that each time I need it.
But how can I do that? I tried to do this, but it didn't work:
var settings = {
legend: {
enabled:false
},
yAxis: {
title: {
text: 'Impact'
}
},
tooltip: {
formatter: function() {
return 'Impact: '+ this.y +' points'
}
},
credits: {
enabled: false
},
exporting: {
enabled: false
}
};
jQuery(document).ready(function() {
new Highcharts.Chart({
chart: {
renderTo: 'chart',
defaultSeriesType: 'column',
width: 450
},
title: {
text: 'Comparison of Impact for Male age 23'
},
xAxis: {
categories: [
'Good brain',
'Good humor',
'Good will'
],
},
series: [{
data: [5,2,8]
}],
settings // <-- HERE
});
});
Share
Improve this question
edited Aug 5, 2010 at 14:58
Crescent Fresh
117k27 gold badges157 silver badges140 bronze badges
asked Aug 5, 2010 at 14:54
Mikkel RevMikkel Rev
9013 gold badges14 silver badges31 bronze badges
1
-
Currently the object looks like
obj.settings.exporting.enabled
. Do you want it to beobj.exporting.enabled
? – slebetman Commented Aug 5, 2010 at 15:00
2 Answers
Reset to default 5jQuerys .extend()
method is what you are looking for.
For instance
$.extend(true, settings, {
yAxis: {
title: {
text: 'Impact NEW'
}
}
});
Would just replace the yAxis
part in your settings object
Reference: .extend()
jQuery.extend(settings,new)
this merges objects full info, it overwrites/merges new to settings
I've got a big JavaScript object literal I am reusing with very few modifications each time. It would be good to make a variable containing most of the object, then reference that each time I need it.
But how can I do that? I tried to do this, but it didn't work:
var settings = {
legend: {
enabled:false
},
yAxis: {
title: {
text: 'Impact'
}
},
tooltip: {
formatter: function() {
return 'Impact: '+ this.y +' points'
}
},
credits: {
enabled: false
},
exporting: {
enabled: false
}
};
jQuery(document).ready(function() {
new Highcharts.Chart({
chart: {
renderTo: 'chart',
defaultSeriesType: 'column',
width: 450
},
title: {
text: 'Comparison of Impact for Male age 23'
},
xAxis: {
categories: [
'Good brain',
'Good humor',
'Good will'
],
},
series: [{
data: [5,2,8]
}],
settings // <-- HERE
});
});
I've got a big JavaScript object literal I am reusing with very few modifications each time. It would be good to make a variable containing most of the object, then reference that each time I need it.
But how can I do that? I tried to do this, but it didn't work:
var settings = {
legend: {
enabled:false
},
yAxis: {
title: {
text: 'Impact'
}
},
tooltip: {
formatter: function() {
return 'Impact: '+ this.y +' points'
}
},
credits: {
enabled: false
},
exporting: {
enabled: false
}
};
jQuery(document).ready(function() {
new Highcharts.Chart({
chart: {
renderTo: 'chart',
defaultSeriesType: 'column',
width: 450
},
title: {
text: 'Comparison of Impact for Male age 23'
},
xAxis: {
categories: [
'Good brain',
'Good humor',
'Good will'
],
},
series: [{
data: [5,2,8]
}],
settings // <-- HERE
});
});
Share
Improve this question
edited Aug 5, 2010 at 14:58
Crescent Fresh
117k27 gold badges157 silver badges140 bronze badges
asked Aug 5, 2010 at 14:54
Mikkel RevMikkel Rev
9013 gold badges14 silver badges31 bronze badges
1
-
Currently the object looks like
obj.settings.exporting.enabled
. Do you want it to beobj.exporting.enabled
? – slebetman Commented Aug 5, 2010 at 15:00
2 Answers
Reset to default 5jQuerys .extend()
method is what you are looking for.
For instance
$.extend(true, settings, {
yAxis: {
title: {
text: 'Impact NEW'
}
}
});
Would just replace the yAxis
part in your settings object
Reference: .extend()
jQuery.extend(settings,new)
this merges objects full info, it overwrites/merges new to settings
本文标签: jqueryMerge two JavaScript objectsStack Overflow
版权声明:本文标题:jquery - Merge two JavaScript objects - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745611749a2159040.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论