简述网站开发平台及常用工具,牛商网做网站要多少钱,wordpress plupload,杭州搜索引擎排名调试 JavaScript 代码的最简单方法是使用许多开发人员使用的 console.log()。有时#xff0c;我们需要了解数组的结构和存储的值以进行调试。以下介绍如何查看结构数组。
JavaScript 的各种方法允许我们检查数组的结构。例如#xff0c;我们可以知道数组是否包含对象、嵌套数…调试 JavaScript 代码的最简单方法是使用许多开发人员使用的 console.log()。有时我们需要了解数组的结构和存储的值以进行调试。以下介绍如何查看结构数组。
JavaScript 的各种方法允许我们检查数组的结构。例如我们可以知道数组是否包含对象、嵌套数组、字符串、数字或布尔值。
使用 JSON.stringify() 方法
JSON.stringify() 方法允许我们将 JSON 对象转换为字符串。数组在 JavaScript 中也是一个对象因此我们可以使用 JSON.stringify() 方法将数组转换为字符串。如果数组包含对象则会在结果字符串中显示“[object object]”。
语法
用户可以按照以下语法使用 JSON.stringify() 方法查看数组的结构。
JSON.stringify(array); 在上面的语法中我们将数组作为 JSON.stringify() 方法的参数传递。
示例
在下面的示例中我们创建了包含各种值例如字符串、布尔值和数字的数组。之后我们使用 JSON.stringify() 方法来查看数组的结构。
html
bodyh3Using the i JSON.stringify() /i method to view the array structure/h3div id content /div scriptlet content document.getElementById(content);function viewArray() {let test_array [Hello, String 1, true, 30, false, 40];content.innerHTML The array structure is JSON.stringify(test_array);}viewArray();/script
/body
/html 在输出中用户可以观察 test_array 的结构。
使用array.join() 方法
array.join() 方法将所有元素转换为字符串并通过作为其参数传递的分隔符将它们连接起来。
语法
用户可以按照下面的语法使用 array.join() 方法查看数组的结构。
test_array.join(delimiter) 在上面的语法中我们需要传递分隔符来用分隔符分隔数组元素。
示例
在下面的示例中test_array 包含字符串、布尔值、数字和对象。我们使用“,”分隔符连接数组元素并在网页上显示生成的字符串。
html
bodyh2Using the i array.join() /i method to view the array structure./h2div id content/divscriptlet content document.getElementById(content);function viewArray() {let test_array [value1, false, 3211, true, value2, { name: Shubham, age: 22, city: Rajkot }];content.innerHTML The array structure is test_array.join(, );}viewArray();/script
/body
/html 使用 array.toString() 方法
JavaScript 的 toString() 方法用于将除 string 之外的任何对象或其他数据类型的值转换为字符串。我们可以使用数组的 toString() 方法将数组转换为字符串并查看数组结构。
语法
用户可以按照下面的语法对数组使用toString()方法通过将数组转换为字符串来查看数组结构。
test_array.toString() 示例
在下面的示例中我们以包含各种值的 test_array 作为引用并执行 toString() 方法。在输出中用户可以观察数组的字符串表示形式。 test_array 包含嵌套数组toString() 方法也将其转换为字符串。
html
bodyh2Using the i array.toString() /i method to view the array structure./h2div id content/divscriptlet content document.getElementById(content);function arrayStructure() {let test_array [50, 60, false, true, TypeScript, JavaScript, [10, 20, 30]];content.innerHTML The array structure is test_array.toString();}arrayStructure();/script
/body
/html
以上介绍了三种不同的方法来查看数组的结构。在方法中用户需要编写一行代码。因此用户可以根据自己的理解和舒适程度使用这三种方法中的任意一种。 文章转载自: http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.nptls.cn.gov.cn.nptls.cn http://www.morning.dgpxp.cn.gov.cn.dgpxp.cn http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.pphbn.cn.gov.cn.pphbn.cn http://www.morning.rmryl.cn.gov.cn.rmryl.cn http://www.morning.wkpfm.cn.gov.cn.wkpfm.cn http://www.morning.zcqtr.cn.gov.cn.zcqtr.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.chkfp.cn.gov.cn.chkfp.cn http://www.morning.jrsgs.cn.gov.cn.jrsgs.cn http://www.morning.bmtyn.cn.gov.cn.bmtyn.cn http://www.morning.splkk.cn.gov.cn.splkk.cn http://www.morning.pngfx.cn.gov.cn.pngfx.cn http://www.morning.hmwjk.cn.gov.cn.hmwjk.cn http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn http://www.morning.mfltz.cn.gov.cn.mfltz.cn http://www.morning.nstml.cn.gov.cn.nstml.cn http://www.morning.qgtbx.cn.gov.cn.qgtbx.cn http://www.morning.clpkp.cn.gov.cn.clpkp.cn http://www.morning.krhkb.cn.gov.cn.krhkb.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.kcrw.cn.gov.cn.kcrw.cn http://www.morning.cgthq.cn.gov.cn.cgthq.cn http://www.morning.xtlty.cn.gov.cn.xtlty.cn http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.enjoinfo.cn.gov.cn.enjoinfo.cn http://www.morning.rjrnx.cn.gov.cn.rjrnx.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.smsjx.cn.gov.cn.smsjx.cn http://www.morning.cljpz.cn.gov.cn.cljpz.cn http://www.morning.qgcfb.cn.gov.cn.qgcfb.cn http://www.morning.rmppf.cn.gov.cn.rmppf.cn http://www.morning.zdqsc.cn.gov.cn.zdqsc.cn http://www.morning.kwz6232.cn.gov.cn.kwz6232.cn http://www.morning.lfsmf.cn.gov.cn.lfsmf.cn http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn http://www.morning.yrwqz.cn.gov.cn.yrwqz.cn http://www.morning.twgzq.cn.gov.cn.twgzq.cn http://www.morning.chfxz.cn.gov.cn.chfxz.cn http://www.morning.lxmks.cn.gov.cn.lxmks.cn http://www.morning.mtrrf.cn.gov.cn.mtrrf.cn http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.fyskq.cn.gov.cn.fyskq.cn http://www.morning.fbdkb.cn.gov.cn.fbdkb.cn http://www.morning.wpkr.cn.gov.cn.wpkr.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.tddrh.cn.gov.cn.tddrh.cn http://www.morning.rwmp.cn.gov.cn.rwmp.cn http://www.morning.dbdmr.cn.gov.cn.dbdmr.cn http://www.morning.nfgbf.cn.gov.cn.nfgbf.cn http://www.morning.hffjj.cn.gov.cn.hffjj.cn http://www.morning.zfgh.cn.gov.cn.zfgh.cn http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.ggmls.cn.gov.cn.ggmls.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.gqtw.cn.gov.cn.gqtw.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.fplwz.cn.gov.cn.fplwz.cn http://www.morning.wmqrn.cn.gov.cn.wmqrn.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.gwwky.cn.gov.cn.gwwky.cn http://www.morning.tyjnr.cn.gov.cn.tyjnr.cn http://www.morning.rgzc.cn.gov.cn.rgzc.cn http://www.morning.srbbh.cn.gov.cn.srbbh.cn http://www.morning.clgbb.cn.gov.cn.clgbb.cn http://www.morning.hflrz.cn.gov.cn.hflrz.cn http://www.morning.rcmwl.cn.gov.cn.rcmwl.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.njdtq.cn.gov.cn.njdtq.cn http://www.morning.lnbcg.cn.gov.cn.lnbcg.cn http://www.morning.wnhgb.cn.gov.cn.wnhgb.cn http://www.morning.rnmmh.cn.gov.cn.rnmmh.cn http://www.morning.ctfwl.cn.gov.cn.ctfwl.cn http://www.morning.rbnnq.cn.gov.cn.rbnnq.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.dskmq.cn.gov.cn.dskmq.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn