最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - Merge two JavaScript objects - Stack Overflow

matteradmin9PV0评论

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 be obj.exporting.enabled ? – slebetman Commented Aug 5, 2010 at 15:00
Add a ment  | 

2 Answers 2

Reset to default 5

jQuerys .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

Post a comment

comment list (0)

  1. No comments so far