JS字符串与数组的相互转换

JS字符串与数组的相互转换

在今天的工作中遇到一个问题,需要将string字符串转换为数组,在此记录一下。

博客参考文章:https://www.cnblogs.com/smzd/p/11792878.html

1、字符串转换为数组

//customerLabelName="实施前、实施中、实施后",以split的字符进行切割
var customerLabelNameResult = data[i].customerLabelName.split("、");
var customerLabelColorNameResult = data[i].customerLabelColorName.split("、");
//输出结果为:customerLabelNameResult:实施前,实施中,实施后
console.log("customerLabelNameResult:" + customerLabelNameResult);
for (let j = 0; j < customerLabelNameResult.length; j++) {
    html += "<div style='" + customerLabelColorNameResult[j] + "'>" + customerLabelNameResult[j] + "</div>"
}

2、数组转换为字符串

//customerLabelNameResult=[实施前,实施中,实施后]
var arrayToString = customerLabelNameResult.join(';');
//输出结果为:arrayToString:实施前;实施中;实施后
console.log("arrayToString:" + arrayToString);
自我控制是最强者的本能-萧伯纳
原文地址:https://www.cnblogs.com/CF1314/p/13790363.html