最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何在NodeJs中将字符串转换为数组

网站源码admin17浏览0评论

如何在NodeJs中将字符串转换为数组

如何在NodeJs中将字符串转换为数组

我有这个字符串:

    var d = [
    '[(not set),20200409,103,0.0]',
    '[(not set),20200410,112,0.0]',
    '[(not set),20200411,56,0.0]',
    '[(not set),20200412,58,0.0]',
    '[(not set),20200413,108,0.0]',
    '[(not set),20200414,91,0.0]'];

但是我希望通过转换字符串d来获得这种类型的数组处理后看起来应该像。

    var processd_array = [
     ['(not set)',20200409,103,0.0],
     ['(not set)',20200410,112,0.0],
     ['(not set)',20200411,56,0.0],
     ['(not set)',20200412,58,0.0],
     ['(not set)',20200413,108,0.0],
     ['(not set)',20200414,91,0.0] ];

如何在Node.js中做到这一点?

回答如下:

首先,让我们将d数组中的每个元素分割成碎片。

var d= [
  '[(not set),20200409,103,0.0]',
  '[(not set),20200410,112,0.0]',
  '[(not set),20200411,56,0.0]',
  '[(not set),20200412,58,0.0]',
  '[(not set),20200413,108,0.0]',
  '[(not set),20200414,91,0.0]'];
 
const result = d.map(item => item.split(','));

console.log(result);
发布评论

评论列表(0)

  1. 暂无评论