简体   繁体   English

合并两个JavaScript对象

[英]Merge two JavaScript objects

I've got a big JavaScript object literal I am reusing with very few modifications each time. 我有一个很大的JavaScript对象常量,每次都很少修改就可以重复使用。 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
 });
}); 

jQuerys .extend() method is what you are looking for. jQuerys .extend()方法就是您想要的。

For instance 例如

$.extend(true, settings, {
   yAxis: {
     title: {
       text: 'Impact NEW'
     }
   }
});

Would just replace the yAxis part in your settings object 只需替换settings objectyAxis部分

Reference: .extend() 参考: .extend()

jQuery.extend(settings,new)合并对象的完整信息 ,它覆盖/合并新的设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM