用整数和字符串分隔数组

我有以下数组,它返回值如下:


0: data: (2) [10000, "Vinil s/ pó"] name: "Janeiro"

我正在尝试以这种方式拆分:


var series1 = series.split(",");


var series2 = series1[1] + "," + series1[2];

但它给了我以下错误:


未捕获的类型错误:series.split 不是一个函数


生成数组的代码


var series = [],

    len = data.length,

    i = 0;

    

 for(i;i<len;i++){

    series.push({

        name: 'Janeiro',

        data:[data[i][7], data[i][3]]

    });

}

链接绘制图形链接


拉莫斯之舞
浏览 119回答 2
2回答

RISEBY

您不需要拆分任何东西,您的数据已经在数组的单独条目中,位于series[index].data[0](数字)和series[index].data[1](字符串)处。所以你可以访问循环中的那些,例如:// (`i` is already declared in the OP's code)for (i = 0; i < series.length; ++i) {&nbsp; &nbsp; var num = series[i].data[0];&nbsp; &nbsp; var str = series[i].data[1];&nbsp; &nbsp; console.log(num, str);}现场示例:var data = [&nbsp; &nbsp; [,,,"Vinil s/ pó",,,,10000],&nbsp; &nbsp; [,,,"Another value",,,,20000],];var series = [],&nbsp; &nbsp; len = data.length,&nbsp; &nbsp; i = 0;&nbsp; &nbsp;&nbsp;&nbsp;for(i;i<len;i++){&nbsp; &nbsp; series.push({&nbsp; &nbsp; &nbsp; &nbsp; name: 'Janeiro',&nbsp; &nbsp; &nbsp; &nbsp; data:[data[i][7], data[i][3]]&nbsp; &nbsp; });}// Using each entry:for (i = 0; i < series.length; ++i) {&nbsp; &nbsp; var num = series[i].data[0];&nbsp; &nbsp; var str = series[i].data[1];&nbsp; &nbsp; console.log(num, str);}或者使用 ES2015+ 语言特性(for-of、解构和const):// Using each entryfor (const {data: [num, str]} of series) {&nbsp; &nbsp; console.log(num, str);}现场示例:var data = [&nbsp; &nbsp; [,,,"Vinil s/ pó",,,,10000],&nbsp; &nbsp; [,,,"Another value",,,,20000],];var series = [],&nbsp; &nbsp; len = data.length,&nbsp; &nbsp; i = 0;&nbsp; &nbsp;&nbsp;&nbsp;for(i;i<len;i++){&nbsp; &nbsp; series.push({&nbsp; &nbsp; &nbsp; &nbsp; name: 'Janeiro',&nbsp; &nbsp; &nbsp; &nbsp; data:[data[i][7], data[i][3]]&nbsp; &nbsp; });}// Using each entryfor (const {data: [num, str]} of series) {&nbsp; &nbsp; console.log(num, str);}

交互式爱情

Series 似乎是一个对象数组,因为 split 是一个 String 方法,您不能在数组或对象上使用它。在每个对象中,您都有指向数组的关键数据,因此您不必按照您尝试的方式拆分它。只需访问 series[index].data[innerArrIndex]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript