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);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM