简体   繁体   English

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

[英]How to use foreach in javascript with my array?

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

I have that array by use get method and i need to write by 'for' in different category by type ? 我有使用get方法的数组,我需要按类型在'for'中按不同的类别编写? How to use for function correct? 如何正确使用功能? where is my mistake? 我的错在哪里?

Your object is invalid this is ending after your first brackets, when you'll have changed it this should loop through 您的对象无效,这将在您的第一个括号后结束,当您更改它时,它应该循环

for(let obj in myObj){

}

and to get your vals thats more like myObj[obj].type 并使你的val更像myObj [obj] .type

myObj contains no property named cars . myObj包含名为cars属性。 You could make myObj an array and iterate over it : 你可以让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> 

You can use the map property of Array to achieve this more easily. 您可以使用Arraymap属性来更轻松地实现此目的。 Check the code below. 检查下面的代码。 Also I would suggest using some kind of block level html element to render this data since you are kind of creating a list. 另外我建议使用某种块级html元素来渲染这些数据,因为你有点创建一个列表。 So I use a li and br for extra line space instead of only br . 因此我使用libr来获得额外的行空间而不是仅仅br Although the extra space is better provided using CSS. 尽管使用CSS可以更好地提供额外的空间。

 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; 
 <p id="demo"></p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM