【问题标题】:how to merge two objects如何合并两个对象
【发布时间】:2013-04-08 09:51:40
【问题描述】:

我有两个对象

var defPathValues={'id':'','fill':'','fill-opacity':'','stroke-width':'','stroke-linecap':'','stroke-linejoin':'',
                        'stroke':'','d':'','transform':''};

var options={'id':'ele1', 'fill':'white'};

我想合并这两个对象并从 defPathValues 中删除其他空属性(即基于我们传递的选项)。

合并后我想在 svg 路径元素中设置这些属性。

在那之前我喜欢

var path = document.createElementNS(this.svgLink, "path");
        $(path).attr({
            'id': this.SvgObject.id+ "_" + series.Name + "_"+ seriesIndex,
            'fill': 'none',
            'stroke-width': style.SeriesStyle.BorderWidth,
            'stroke': style.SeriesInterior,
            'stroke-linecap': style.SeriesStyle.LineCap,
            'stroke-linejoin':style.SeriesStyle.LineJoin,     
            'd': direction,

        });

而不是我想将对象直接设置为路径元素的 attr。我该怎么做?

【问题讨论】:

    标签: jquery svg jquery-svg


    【解决方案1】:

    这可能不是最优雅的解决方案,但我相信它会起作用:我假设您在此代码运行之前已将 defPathValues 和选项定义为 JS 对象。

    for(var i in options) defPathValues[i] = options[i];
    
    var newDefPathValues = {};
    for(var i in defPathValues) {
        if(typeof defPathValues[i] == 'undefined' || defPathValues[i] == '' || defPathValues[i] == null) continue;
        newDefPathValues[i] = defPathValues[i];
    }
    defPathValues = newDefPathValues;
    

    【讨论】:

      猜你喜欢
      • 2011-01-01
      • 1970-01-01
      • 2019-06-19
      • 2022-01-04
      • 2011-03-19
      • 1970-01-01
      • 2019-10-08
      相关资源
      最近更新 更多