如何在我的数组中使用JavaScript中的foreach?

时间:2019-06-03 08:20:09

标签: javascript

<p id="demo"></p>

<script>
var myObj, i, x = "";
myObj = {
      "__type": "DocumentLibraryHelps.Methods.Instruction",
      "name": "IT-DSM-WI-003-RU_Управление BI Cloud_Ред.03.pdf",
      "url": "https://cloud.bi-group.org/instr/BI Cloud/IT-DSM-WI-003-RU_Управление BI Cloud_Ред.03.pdf?web=1",
      "type": "Microsoft.SharePoint.SPFile",
      "category": "Cloud",
      "section": "Cloud"
    },
    {
      "__type": "DocumentLibraryHelps.Methods.Instruction",
      "name": "IT-DSM-WI-002-RU_Разграничение прав доступа к BI Cloud_Ред.02.pdf",
      "url": "https://cloud.bi-group.org/instr/BI Cloud/IT-DSM-WI-002-RU_Разграничение прав доступа к BI Cloud_Ред.02.pdf?web=1",
      "type": "Microsoft.SharePoint.SPFile",
      "category": "Cloud",
      "section": "Cloud"
    },
    {
      "__type": "DocumentLibraryHelps.Methods.Instruction",
      "name": "Инструкция_Галерея.docx",
      "url": "https://cloud.bi-group.org/instr/Life/Инструкция_Галерея.docx?web=1",
      "type": "Microsoft.SharePoint.SPFile",
      "category": "ERP: Первые шаги",
      "section": "1C: ERP"
    };

for (i = 0; i < myObj.cars.length; i++) {
  x += myObj.name[i]  +=  myObj.url[i]  += myObj.type[i] + "<br>";
}
document.getElementById("demo").innerHTML = x;
</script>

我通过使用get方法获得了该数组,我需要按类型在不同类别中用“ for”编写?如何正确使用功能?我的错误在哪里?

3 个答案:

答案 0 :(得分:0)

您的对象无效,这将在您将其更改为第一个方括号后结束 这应该遍历

for(let obj in myObj){

}

并使您的val更像 myObj [obj] .type

答案 1 :(得分:0)

myObj不包含名为cars的属性。 您可以将myObj制成数组并对其进行迭代:

var myObj, i, x = "";
myObj = [{
      "__type": "DocumentLibraryHelps.Methods.Instruction",
      "name": "IT-DSM-WI-003-RU_Управление BI Cloud_Ред.03.pdf",
      "url": "https://cloud.bi-group.org/instr/BI Cloud/IT-DSM-WI-003-RU_Управление BI Cloud_Ред.03.pdf?web=1",
      "type": "Microsoft.SharePoint.SPFile",
      "category": "Cloud",
      "section": "Cloud"
    },
    {
      "__type": "DocumentLibraryHelps.Methods.Instruction",
      "name": "IT-DSM-WI-002-RU_Разграничение прав доступа к BI Cloud_Ред.02.pdf",
      "url": "https://cloud.bi-group.org/instr/BI Cloud/IT-DSM-WI-002-RU_Разграничение прав доступа к BI Cloud_Ред.02.pdf?web=1",
      "type": "Microsoft.SharePoint.SPFile",
      "category": "Cloud",
      "section": "Cloud"
    },
    {
      "__type": "DocumentLibraryHelps.Methods.Instruction",
      "name": "Инструкция_Галерея.docx",
      "url": "https://cloud.bi-group.org/instr/Life/Инструкция_Галерея.docx?web=1",
      "type": "Microsoft.SharePoint.SPFile",
      "category": "ERP: Первые шаги",
      "section": "1C: ERP"
    }];

for (i = 0; i < myObj.length; i++) {
  x += myObj[i].name  +=  myObj[i].url  += myObj[i].type + "<br>";
}
document.getElementById("demo").innerHTML = x;
<p id="demo"></p>

答案 2 :(得分:0)

您可以使用map的{​​{1}}属性来更轻松地实现此目的。检查下面的代码。 我也建议您使用某种块级html元素来呈现此数据,因为您正在创建列表。因此,我使用Arrayli来增加行距,而不仅仅是br。尽管使用CSS可以更好地提供额外的空间。

br
const myObj = [{
      "__type": "DocumentLibraryHelps.Methods.Instruction",
      "name": "IT-DSM-WI-003-RU_Управление BI Cloud_Ред.03.pdf",
      "url": "https://cloud.bi-group.org/instr/BI Cloud/IT-DSM-WI-003-RU_Управление BI Cloud_Ред.03.pdf?web=1",
      "type": "Microsoft.SharePoint.SPFile",
      "category": "Cloud",
      "section": "Cloud"
    },
    {
      "__type": "DocumentLibraryHelps.Methods.Instruction",
      "name": "IT-DSM-WI-002-RU_Разграничение прав доступа к BI Cloud_Ред.02.pdf",
      "url": "https://cloud.bi-group.org/instr/BI Cloud/IT-DSM-WI-002-RU_Разграничение прав доступа к BI Cloud_Ред.02.pdf?web=1",
      "type": "Microsoft.SharePoint.SPFile",
      "category": "Cloud",
      "section": "Cloud"
    },
    {
      "__type": "DocumentLibraryHelps.Methods.Instruction",
      "name": "Инструкция_Галерея.docx",
      "url": "https://cloud.bi-group.org/instr/Life/Инструкция_Галерея.docx?web=1",
      "type": "Microsoft.SharePoint.SPFile",
      "category": "ERP: Первые шаги",
      "section": "1C: ERP"
    }];
    
const txt = myObj.map(obj=>'<li>'+obj.name+obj.type+obj.url+'</li><br>').join('');
document.getElementById("demo").innerHTML = txt;