如何解析 JavaScript 数组中的字符串?
以下是解析 JavaScript 数组中字符串的代码 −
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } </style> </head> <body> <h1>Parse a string from a JavaScript array</h1> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3>Click the above button to convert the array arr to string</h3> <script> let BtnEle = document.querySelector(".Btn"); let resEle = document.querySelector(".result"); let arr = ["Cow", "Lion", "Tiger", "Dog", "Cat"]; BtnEle.addEventListener("click", () => { resEle.innerHTML = arr.toString(); }); </script> </body> </html>
输出
以上代码将产生以下输出 −
点击“单击此处”按钮 −
广告