将数组传递给函数。Javascript
我是一个新手,正在尝试将字符串传递到函数中,或者其他任何东西 下面的数据和项目下有一些文字将数组传递给函数。Javascript,javascript,arrays,Javascript,Arrays,我是一个新手,正在尝试将字符串传递到函数中,或者其他任何东西 下面的数据和项目下有一些文字 “工作历史” “项目” “教育” 这是一个名为index.js的文件。再往下一点就是BubbleInfo。将文本输入BubbleInfo以便将其传递给函数的语法是什么。我已经试了好几天了,但是都没有正确的语法来抓取文本。我总是得到一个错误或未定义 $(document).ready(function () { var bubbleChart = new d3.svg.BubbleChart({
- “工作历史”
- “项目”
- “教育”
$(document).ready(function () {
var bubbleChart = new d3.svg.BubbleChart({
supportResponsive: true,
//container: => use @default
size: 600,
//viewBoxSize: => use @default
innerRadius: 600 / 3.5,
//outerRadius: => use @default
radiusMin: 50,
//radiusMax: use @default
//intersectDelta: use @default
//intersectInc: use @default
//circleColor: use @default
data: {
items: [
{text: "Work History", count: "3"},
{text: "Projects", count: "4"},
{text: "Education", count: "3"},
{text: "Work Locations", count: "4"},
],
eval: function (item) {return item.count;},
classed: function (item) {return item.text.split(" ").join("");}
},
plugins: [
{
name: "central-click",
options: {
text: ("See more detail"),
BubbleInfo: (**data.items.text**),
style: {
"font-size": "12px",
"font-style": "italic",
"font-family": "Source Sans Pro, sans-serif",
//"font-weight": "700",
"text-anchor": "middle",
"fill": "white"
},
attr: {dy: "65px"},
centralClick: function() {
// window.alert("Here is details!!");
}
}
},
{
name: "lines",
options: {
format: [
{// Line #0
textField: "count",
classed: {count: true},
style: {
"font-size": "28px",
"font-family": "Source Sans Pro, sans-serif",
"text-anchor": "middle",
fill: "white"
},
attr: {
dy: "0px",
x: function (d) {return d.cx;},
y: function (d) {return d.cy;}
}
},
{// Line #1
textField: "text",
classed: {text: true},
style: {
"font-size": "14px",
"font-family": "Source Sans Pro, sans-serif",
"text-anchor": "middle",
fill: "white"
},
attr: {
dy: "20px",
x: function (d) {return d.cx;},
y: function (d) {return d.cy;}
}
}
],
centralFormat: [
{// Line #0
style: {"font-size": "50px"},
attr: {}
},
{// Line #1
style: {"font-size": "30px"},
attr: {dy: "40px"}
}
]
}
}]
});
});
如果我正确理解了您的问题,并且您只需要一个包含所有文本字段的数组,那么您只需使用array.prototype.map
var data = {
items: [{
text: "Work History",
count: "3"
}, {
text: "Projects",
count: "4"
}, {
text: "Education",
count: "3"
}, {
text: "Work Locations",
count: "4"
}]
};
var texts = data.items.map(function(item) {
return item.text;
}); //["Work History", "Projects", "Education", "Work Locations"]
var bubbleChart = new d3.svg.BubbleChart({
///...
data: data, //Add methods to data declaration above if needed
plugins: [{
//...
options: {
BubbleInfo: texts
}
}]
});
所以您想要一个包含所有文本字段的数组?
数据。items
是一个数组,要传递该数组,请使用。。。等等<代码>数据。项目。数组包含4个对象,要访问这些对象,请使用数据。第一个对象使用项[0]
,第二个对象使用数据。第二个对象使用项[1]
等。然后使用数据。项[0]。文本。如果你想要所有的文本,你必须改变数组,或者把它映射到其他地方。我得到了一个意外标识符的错误。那是因为我必须把它放在代码的其他地方。我应该把代码放在哪里?如果你不显示,你不能指望任何人神奇地知道你把代码放在哪里。解决错误也是一样。对不起,这里是新手。我的错。我把它放在了插件上面:[上面的语法]。你不能在那里声明随机变量,你在一个对象的定义中间。