对函数参数进行循环
Doing a loop on function parameters
这是当前的工作代码:
$.fn.vectorMap('addMap', 'world', {"width":950,"height":550,"paths":{
id:{"path":data.id.path,"name":data.id.name},
pg:{"path":data.pg.path,"name":data.pg.name},
mx:{"path":data.mx.path,"name":data.mx.name},
ee:{"path":data.ee.path,"name":data.ee.name},
dz:{"path":data.dz.path,"name":data.dz.name},
ma:{"path":data.ma.path,"name":data.ma.name},
mr:{"path":data.mr.path,"name":data.mr.name},
sn:{"path":data.sn.path,"name":data.sn.name},
gm:{"path":data.gm.path,"name":data.gm.name},
gw:{"path":data.gw.path,"name":data.gw.name},
gn:{"path":data.gn.path,"name":data.gn.name},
sl:{"path":data.sl.path,"name":data.sl.name},
lr:{"path":data.lr.path,"name":data.lr.name},
ci:{"path":data.ci.path,"name":data.ci.name},
ml:{"path":data.ml.path,"name":data.ml.name},
bf:{"path":data.bf.path,"name":data.bf.name},
ne:{"path":data.ne.path,"name":data.ne.name},
gh:{"path":data.gh.path,"name":data.gh.name},
tg:{"path":data.tg.path,"name":data.tg.name},
bj:{"path":data.bj.path,"name":data.bj.name},
ng:{"path":data.ng.path,"name":data.ng.name},
tn:{"path":data.tn.path,"name":data.tn.name},
ly:{"path":data.ly.path,"name":data.ly.name},
eg:{"path":data.eg.path,"name":data.eg.name},
td:{"path":data.td.path,"name":data.td.name},
sd:{"path":data.sd.path,"name":data.sd.name},
cm:{"path":data.cm.path,"name":data.cm.name},
er:{"path":data.er.path,"name":data.er.name},
dj:{"path":data.dj.path,"name":data.dj.name},
et:{"path":data.et.path,"name":data.et.name},
so:{"path":data.so.path,"name":data.so.name},
ye:{"path":data.ye.path,"name":data.ye.name},
cf:{"path":data.cf.path,"name":data.cf.name},
st:{"path":data.st.path,"name":data.st.name},
gq:{"path":data.gq.path,"name":data.gq.name},
ga:{"path":data.ga.path,"name":data.ga.name},
cg:{"path":data.cg.path,"name":data.cg.name},
ao:{"path":data.ao.path,"name":data.ao.name},
cd:{"path":data.cd.path,"name":data.cd.name},
rw:{"path":data.rw.path,"name":data.rw.name},
bi:{"path":data.bi.path,"name":data.bi.name},
ug:{"path":data.ug.path,"name":data.ug.name},
ke:{"path":data.ke.path,"name":data.ke.name},
tz:{"path":data.tz.path,"name":data.tz.name},
zm:{"path":data.zm.path,"name":data.zm.name},
mw:{"path":data.mw.path,"name":data.mw.name},
mz:{"path":data.mz.path,"name":data.mz.name},
zw:{"path":data.zw.path,"name":data.zw.name},
na:{"path":data.na.path,"name":data.na.name},
bw:{"path":data.bw.path,"name":data.bw.name},
sz:{"path":data.sz.path,"name":data.sz.name},
ls:{"path":data.ls.path,"name":data.ls.name},
za:{"path":data.za.path,"name":data.za.name},
gl:{"path":data.gl.path,"name":data.gl.name},
au:{"path":data.au.path,"name":data.au.name},
nz:{"path":data.nz.path,"name":data.nz.name},
nc:{"path":data.nc.path,"name":data.nc.name},
my:{"path":data.my.path,"name":data.my.name},
bn:{"path":data.bn.path,"name":data.bn.name},
tl:{"path":data.tl.path,"name":data.tl.name},
sb:{"path":data.sb.path,"name":data.sb.name},
vu:{"path":data.vu.path,"name":data.vu.name},
fj:{"path":data.fj.path,"name":data.fj.name},
ph:{"path":data.ph.path,"name":data.ph.name},
cn:{"path":data.cn.path,"name":data.cn.name},
tw:{"path":data.tw.path,"name":data.tw.name},
jp:{"path":data.jp.path,"name":data.jp.name},
ru:{"path":data.ru.path,"name":data.ru.name},
us:{"path":data.us.path,"name":data.us.name},
mu:{"path":data.mu.path,"name":data.mu.name},
re:{"path":data.re.path,"name":data.re.name},
mg:{"path":data.mg.path,"name":data.mg.name},
km:{"path":data.km.path,"name":data.km.name},
sc:{"path":data.sc.path,"name":data.sc.name},
mv:{"path":data.mv.path,"name":data.mv.name},
pt:{"path":data.pt.path,"name":data.pt.name},
es:{"path":data.es.path,"name":data.es.name},
cv:{"path":data.cv.path,"name":data.cv.name},
pf:{"path":data.pf.path,"name":data.pf.name},
kn:{"path":data.kn.path,"name":data.kn.name},
ag:{"path":data.ag.path,"name":data.ag.name},
dm:{"path":data.dm.path,"name":data.dm.name},
lc:{"path":data.lc.path,"name":data.lc.name},
bb:{"path":data.bb.path,"name":data.bb.name},
gd:{"path":data.gd.path,"name":data.gd.name},
tt:{"path":data.tt.path,"name":data.tt.name},
do:{"path":data.do.path,"name":data.do.name},
ht:{"path":data.ht.path,"name":data.ht.name},
fk:{"path":data.fk.path,"name":data.fk.name},
is:{"path":data.is.path,"name":data.is.name},
no:{"path":data.no.path,"name":data.no.name},
lk:{"path":data.lk.path,"name":data.lk.name},
cu:{"path":data.cu.path,"name":data.cu.name},
bs:{"path":data.bs.path,"name":data.bs.name},
jm:{"path":data.jm.path,"name":data.jm.name},
ec:{"path":data.ec.path,"name":data.ec.name},
ca:{"path":data.ca.path,"name":data.ca.name},
gt:{"path":data.gt.path,"name":data.gt.name},
hn:{"path":data.hn.path,"name":data.hn.name},
sv:{"path":data.sv.path,"name":data.sv.name},
ni:{"path":data.ni.path,"name":data.ni.name},
cr:{"path":data.cr.path,"name":data.cr.name},
gr:{"path":data.gr.path,"name":data.gr.name}}
});
如您所见,我只是将所有元素列为数据。所以,我的问题是:我可以对数据进行循环以使其只有一行完全相同吗?那怎么办呢,因为元素是vectorMap函数的一个参数。
预先感谢您的帮助。
要更简单地创建此数据结构,您可以遍历 data
中的键并构建所需的对象数组,如下所示:
var options = {
width: 950,
height: 550,
paths: {}
};
for (var key in data) {
options.paths[key] = {
path: data[key].path,
name: data[key].name
}
}
$.fn.vectorMap('addMap', 'world', options);
您可以使用 Object.assign
和 Object.entries
在一个表达式中执行此操作:
"paths": Object.assign(...Object.entries(data).map( ([k,v]) => ({ [k]: v }) ))
但是请注意,这将注入整个 data.xx
对象,包括它们可能具有的除 path
和 name
之外的任何其他属性。如果这是不需要的,那么:
"paths": Object.assign(...Object.entries(data).map(
([k, {path,name}]) => ({ [k]: {path, name} })
))
这是当前的工作代码:
$.fn.vectorMap('addMap', 'world', {"width":950,"height":550,"paths":{
id:{"path":data.id.path,"name":data.id.name},
pg:{"path":data.pg.path,"name":data.pg.name},
mx:{"path":data.mx.path,"name":data.mx.name},
ee:{"path":data.ee.path,"name":data.ee.name},
dz:{"path":data.dz.path,"name":data.dz.name},
ma:{"path":data.ma.path,"name":data.ma.name},
mr:{"path":data.mr.path,"name":data.mr.name},
sn:{"path":data.sn.path,"name":data.sn.name},
gm:{"path":data.gm.path,"name":data.gm.name},
gw:{"path":data.gw.path,"name":data.gw.name},
gn:{"path":data.gn.path,"name":data.gn.name},
sl:{"path":data.sl.path,"name":data.sl.name},
lr:{"path":data.lr.path,"name":data.lr.name},
ci:{"path":data.ci.path,"name":data.ci.name},
ml:{"path":data.ml.path,"name":data.ml.name},
bf:{"path":data.bf.path,"name":data.bf.name},
ne:{"path":data.ne.path,"name":data.ne.name},
gh:{"path":data.gh.path,"name":data.gh.name},
tg:{"path":data.tg.path,"name":data.tg.name},
bj:{"path":data.bj.path,"name":data.bj.name},
ng:{"path":data.ng.path,"name":data.ng.name},
tn:{"path":data.tn.path,"name":data.tn.name},
ly:{"path":data.ly.path,"name":data.ly.name},
eg:{"path":data.eg.path,"name":data.eg.name},
td:{"path":data.td.path,"name":data.td.name},
sd:{"path":data.sd.path,"name":data.sd.name},
cm:{"path":data.cm.path,"name":data.cm.name},
er:{"path":data.er.path,"name":data.er.name},
dj:{"path":data.dj.path,"name":data.dj.name},
et:{"path":data.et.path,"name":data.et.name},
so:{"path":data.so.path,"name":data.so.name},
ye:{"path":data.ye.path,"name":data.ye.name},
cf:{"path":data.cf.path,"name":data.cf.name},
st:{"path":data.st.path,"name":data.st.name},
gq:{"path":data.gq.path,"name":data.gq.name},
ga:{"path":data.ga.path,"name":data.ga.name},
cg:{"path":data.cg.path,"name":data.cg.name},
ao:{"path":data.ao.path,"name":data.ao.name},
cd:{"path":data.cd.path,"name":data.cd.name},
rw:{"path":data.rw.path,"name":data.rw.name},
bi:{"path":data.bi.path,"name":data.bi.name},
ug:{"path":data.ug.path,"name":data.ug.name},
ke:{"path":data.ke.path,"name":data.ke.name},
tz:{"path":data.tz.path,"name":data.tz.name},
zm:{"path":data.zm.path,"name":data.zm.name},
mw:{"path":data.mw.path,"name":data.mw.name},
mz:{"path":data.mz.path,"name":data.mz.name},
zw:{"path":data.zw.path,"name":data.zw.name},
na:{"path":data.na.path,"name":data.na.name},
bw:{"path":data.bw.path,"name":data.bw.name},
sz:{"path":data.sz.path,"name":data.sz.name},
ls:{"path":data.ls.path,"name":data.ls.name},
za:{"path":data.za.path,"name":data.za.name},
gl:{"path":data.gl.path,"name":data.gl.name},
au:{"path":data.au.path,"name":data.au.name},
nz:{"path":data.nz.path,"name":data.nz.name},
nc:{"path":data.nc.path,"name":data.nc.name},
my:{"path":data.my.path,"name":data.my.name},
bn:{"path":data.bn.path,"name":data.bn.name},
tl:{"path":data.tl.path,"name":data.tl.name},
sb:{"path":data.sb.path,"name":data.sb.name},
vu:{"path":data.vu.path,"name":data.vu.name},
fj:{"path":data.fj.path,"name":data.fj.name},
ph:{"path":data.ph.path,"name":data.ph.name},
cn:{"path":data.cn.path,"name":data.cn.name},
tw:{"path":data.tw.path,"name":data.tw.name},
jp:{"path":data.jp.path,"name":data.jp.name},
ru:{"path":data.ru.path,"name":data.ru.name},
us:{"path":data.us.path,"name":data.us.name},
mu:{"path":data.mu.path,"name":data.mu.name},
re:{"path":data.re.path,"name":data.re.name},
mg:{"path":data.mg.path,"name":data.mg.name},
km:{"path":data.km.path,"name":data.km.name},
sc:{"path":data.sc.path,"name":data.sc.name},
mv:{"path":data.mv.path,"name":data.mv.name},
pt:{"path":data.pt.path,"name":data.pt.name},
es:{"path":data.es.path,"name":data.es.name},
cv:{"path":data.cv.path,"name":data.cv.name},
pf:{"path":data.pf.path,"name":data.pf.name},
kn:{"path":data.kn.path,"name":data.kn.name},
ag:{"path":data.ag.path,"name":data.ag.name},
dm:{"path":data.dm.path,"name":data.dm.name},
lc:{"path":data.lc.path,"name":data.lc.name},
bb:{"path":data.bb.path,"name":data.bb.name},
gd:{"path":data.gd.path,"name":data.gd.name},
tt:{"path":data.tt.path,"name":data.tt.name},
do:{"path":data.do.path,"name":data.do.name},
ht:{"path":data.ht.path,"name":data.ht.name},
fk:{"path":data.fk.path,"name":data.fk.name},
is:{"path":data.is.path,"name":data.is.name},
no:{"path":data.no.path,"name":data.no.name},
lk:{"path":data.lk.path,"name":data.lk.name},
cu:{"path":data.cu.path,"name":data.cu.name},
bs:{"path":data.bs.path,"name":data.bs.name},
jm:{"path":data.jm.path,"name":data.jm.name},
ec:{"path":data.ec.path,"name":data.ec.name},
ca:{"path":data.ca.path,"name":data.ca.name},
gt:{"path":data.gt.path,"name":data.gt.name},
hn:{"path":data.hn.path,"name":data.hn.name},
sv:{"path":data.sv.path,"name":data.sv.name},
ni:{"path":data.ni.path,"name":data.ni.name},
cr:{"path":data.cr.path,"name":data.cr.name},
gr:{"path":data.gr.path,"name":data.gr.name}}
});
如您所见,我只是将所有元素列为数据。所以,我的问题是:我可以对数据进行循环以使其只有一行完全相同吗?那怎么办呢,因为元素是vectorMap函数的一个参数。
预先感谢您的帮助。
要更简单地创建此数据结构,您可以遍历 data
中的键并构建所需的对象数组,如下所示:
var options = {
width: 950,
height: 550,
paths: {}
};
for (var key in data) {
options.paths[key] = {
path: data[key].path,
name: data[key].name
}
}
$.fn.vectorMap('addMap', 'world', options);
您可以使用 Object.assign
和 Object.entries
在一个表达式中执行此操作:
"paths": Object.assign(...Object.entries(data).map( ([k,v]) => ({ [k]: v }) ))
但是请注意,这将注入整个 data.xx
对象,包括它们可能具有的除 path
和 name
之外的任何其他属性。如果这是不需要的,那么:
"paths": Object.assign(...Object.entries(data).map(
([k, {path,name}]) => ({ [k]: {path, name} })
))