将文本框字符串转换为javascript数组
我有一个文本框,其中包含以下内容- [0,0]、[1,0]、[2,0]、[3,0]、[4,0]、[5,1]、[6,6]] 我需要能够将其转换为数组,格式与图形完全相同。我在下面尝试的代码不起作用,因为它在数组中没有方括号将文本框字符串转换为javascript数组,javascript,jquery,arrays,Javascript,Jquery,Arrays,我有一个文本框,其中包含以下内容- [0,0]、[1,0]、[2,0]、[3,0]、[4,0]、[5,1]、[6,6]] 我需要能够将其转换为数组,格式与图形完全相同。我在下面尝试的代码不起作用,因为它在数组中没有方括号 /*****SIMPLE CHART*****/ var flashstring = document.getElementsByName('hfMessageHistory')[0].value; var flash = new Array(
/*****SIMPLE CHART*****/
var flashstring = document.getElementsByName('hfMessageHistory')[0].value;
var flash = new Array();
flash = flashstring.split(",");
for (a in flash) {
flash[a] = parseInt(flash[a], 10) || 0; // Explicitly include base as per Álvaro's comment
}
alert(flash);
function showTooltip(x, y, contents) {
jQuery('<div id="tooltip" class="tooltipflot">' + contents + '</div>').css({
position: 'absolute',
display: 'none',
top: y + 5,
left: x + 5
}).appendTo("body").fadeIn(200);
}
var plot = jQuery.plot(jQuery("#chartplace"),
[{ data: [flash], label: "Messages Sent", color: "#ff6c00" }], {
series: {
lines: { show: true, fill: true, fillColor: { colors: [{ opacity: 0.05 }, { opacity: 0.15 }] } },
points: { show: true }
},
legend: { position: 'nw' },
grid: { hoverable: true, clickable: true, borderColor: '#ccc', borderWidth: 1, labelMargin: 10 },
yaxis: { min: 0, max: 20 }
});
/****简单图表*****/
var flashstring=document.getElementsByName('hfMessageHistory')[0]。值;
var flash=新数组();
flash=flashstring.split(“,”);
为了(一瞬间){
flash[a]=parseInt(flash[a],10)| | 0;//根据阿尔瓦罗的评论显式包含base
}
警报(闪光);
函数显示工具提示(x、y、内容){
jQuery(“”+内容+“”).css({
位置:'绝对',
显示:“无”,
顶部:y+5,
左:x+5
}).appendTo(“body”).fadeIn(200);
}
var plot=jQuery.plot(jQuery(“#chartplace”),
[{data:[flash],标签:“已发送的消息”,颜色:“ff6c00”}{
系列:{
行:{show:true,fill:true,fillColor:{colors:[{opacity:0.05},{opacity:0.15}]},
要点:{show:true}
},
图例:{位置:'nw'},
网格:{hoverable:true,clickable:true,borderColor:'#ccc',borderWidth:1,labelMargin:10},
yaxis:{min:0,max:20}
});
为了避免代码的其他部分(不清楚您在问什么),可以使用括号([])将字符串转换为数组:
为了避免代码的其他部分(不清楚您在问什么),可以使用括号([])将字符串转换为数组:
为了避免代码的其他部分(不清楚您在问什么),可以使用括号([])将字符串转换为数组:
为了避免代码的其他部分(不清楚您在问什么),可以使用括号([])将字符串转换为数组:
字符串是JSON。使用JSON.parse():
字符串是JSON。使用JSON.parse():
字符串是JSON。使用JSON.parse():
字符串是JSON。使用JSON.parse(): 使用: HTML: .使用: HTML: .使用: HTML: .使用: HTML:
var str = '[[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 1], [6, 6]]'
var arr = [str];
var flashstring = document.getElementsByName('hfMessageHistory')[0].value;
var flash = JSON.parse(flashstring);
<input type="text" id="text1" />
<button onclick="parse()">Parse</button>
$('#text1').val('[[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 1], [6, 6]]');
function parse() {
try {
var parsed = JSON.parse($('#text1').val());
console.log(parsed);
} catch (e) {
console.log('parse failed. Check your input');
}
}