从输出中删除第一个字母

时间:2017-01-10 12:22:51

标签: javascript arrays

如何从此字符串中删除第一个字母

d[],e[], [dsh,sj]

我通过分析得到了这个,但现在我需要在每个逗号(,)之前删除第一个字母。所以,我存储它并申请循环,但它给了我错误。

*Uncaught SyntaxError: Unexpected token [*

但我不明白为什么?

编辑:我知道如何删除元素,但在这里我无法声明它

Expected Output :  [],[],[dsh,sj]

1 个答案:

答案 0 :(得分:2)

只是为了确保我们谈论同样的事情:

var s = "hello"; // a String, notice the quotes
var a = [] // an empty Array
var b = ["hello", "bye"] // an Array with 2 elements

如果这是您的输入:



var inpt = "d[],e[], [dsh,sj]";
var otpt = inpt.replace(/.\[/g,"["); // returns '[],[],[dsh,sj]' but as data type String

// this block is not very clean...
var splt = otpt.replace(/],/g, "],,");
splt = splt.split(",,");

var arr = [];
splt.forEach(function(value) {
  value = value.replace(/\[|\]/g,"");
  if (value === "") {
	value = [];
  } else {
	value = value.split(",");
  }
  arr.push(value);
});

console.log(arr); // [[],[],["dsh","sj"]]




我建议您查看一下:http://www.w3schools.com/js/default.asp