如何合并两个对象

时间:2013-04-16 05:33:34

标签: jquery svg jquery-svg

我有两个对象

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。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

这可能不是最优雅的解决方案,但我相信它会起作用:我假设您在此代码运行之前已经将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;