猿问

如何在javascript中解析多行/行数组

我想用多行和多行解析我的数组,我尝试使用 foreach 或 for 循环,但没有成功


var student = [

      {name:"alan",title:"al",platform:"test1",orderNumber:1 },

      {name:"frank",title:"fr", platform:"test2",orderNumber:1},

      {name:"bryan",title:"br", platform:"test3",orderNumber:1}

      ];


for (var i = 0; i < student.length; ++i) {

        window.alert(student[i]);

      }

它不起作用。我怎么能用循环解析这个数组并显示它?多谢


HUX布斯
浏览 166回答 2
2回答

梵蒂冈之花

使用Object.entries()和forEach()迭代对象的属性。var student = [&nbsp; {name:"alan",title:"al",platform:"test1",orderNumber:1 },&nbsp; {name:"frank",title:"fr", platform:"test2",orderNumber:1},&nbsp; {name:"bryan",title:"br", platform:"test3",orderNumber:1}];student.forEach(item => {&nbsp; Object.entries(item).forEach(([key, value]) => {&nbsp; &nbsp; console.log(key + ": " + value);&nbsp; });});

守着星空守着你

这是一个实时链接https://dotnetfiddle.net/aQJP3W<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta name="viewport" content="width=device-width" />&nbsp; &nbsp; <title>Index18</title>&nbsp; &nbsp; <script src="~/Scripts/jquery-3.3.1.min.js"></script>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; $(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#theButton").click(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var student = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "alan", title: "al", platform: "test1", orderNumber: 1 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "frank", title: "fr", platform: "test2", orderNumber: 1 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "bryan", title: "br", platform: "test3", orderNumber: 1 }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.each(student, function (index, value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(value.name + "-" + value.title + "-" + value.platform + "-" + value.orderNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; </script></head><body>&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; <input type="button" value="Go" id="theButton" />&nbsp; &nbsp; </div></body></html>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答