简体   繁体   English

如何遍历api数组中的多个对象

[英]How can I loop through multiple objects in a api array

With my current code, I'm able to loop through a single object to get the data I need. 使用当前的代码,我可以遍历单个对象来获取所需的数据。 I'm running into a problem trying to loop through all of the available objects, so that I can get the data from all objects returned from the ajax search. 我在尝试遍历所有可用对象时遇到问题,以便可以从ajax搜索返回的所有对象中获取数据。

This is the code I am using to search through a single object. 这是我用来搜索单个对象的代码。

$.ajax({
 url: "",
 type: "get",
 async: true,
 contentType: "application/json",
 success: function(data) {
 console.log(data);


   var fields = data[0].Fields ;


 document.getElementById("test").innerHTML = fields
 .map(function(field) {
  return '<p>' + field.DisplayName + ': ' + field.DataValue + '</p>';
  })
 .join('\n');

I tried removing the [0] in the var fields = data[0].fields , but I get map is undefined. 我尝试删除varfields = data [0] .fields中的[0],但得到的地图未定义。

When I tried adding a for loop to search through all available objects I'm pretty sure I set it up wrong. 当我尝试添加for循环以搜索所有可用对象时,我很确定自己设置了错误。 I get cannot read property length of undefined... 我无法读取未定义的属性长度...

  var fields = data.Fields ;
  for(i = 0; i > fields.length; i++){
   document.getElementById("test").innerHTML = fields
 .map(function(field) {
  return '<p>' + field.DisplayName + ': ' + field.DataValue + '</p>';
 })
 .join('\n');
 }

Here is the console.log(data) output 这是console.log(data)输出

(3) [{…}, {…}, {…}]
0: {Fields: Array(8), DocImage: {…}}
1: {Fields: Array(8), DocImage: {…}}
2: {Fields: Array(8), DocImage: {…}}
length: 3
__proto__: Array(0)

From your answer I guess your data is something like: 根据您的回答,我猜您的数据如下:

var data = [{Fields: [{DisplayName: 1, DataValue: 1}, {DisplayName: 2, DataValue: 2}], DocImage: '1'},
            {Fields: [{DisplayName: 11, DataValue: 11}, {DisplayName: 22, DataValue: 22}], DocImage: '2'},
            {Fields: [{DisplayName: 31, DataValue: 31}, {DisplayName: 32, DataValue: 32}], DocImage: '3'}] ;

If that's true your cycle can be: 如果是这样,您的周期可以是:

document.getElementById("test").innerHTML = data.map(function(field) {
    return field.Fields.map(function(e) {
        return '<p>' + e.DisplayName + ': ' + e.DataValue + '</p>';
    }).join('<br/>');
}).join('<br/>');

 var data = [{Fields: [{DisplayName: 1, DataValue: 1}, {DisplayName: 2, DataValue: 2}], DocImage: '1'}, {Fields: [{DisplayName: 11, DataValue: 11}, {DisplayName: 22, DataValue: 22}], DocImage: '2'}, {Fields: [{DisplayName: 31, DataValue: 31}, {DisplayName: 32, DataValue: 32}], DocImage: '3'}] ; document.getElementById("test").innerHTML = data.map(function(field) { return field.Fields.map(function(e) { return '<p>' + e.DisplayName + ': ' + e.DataValue + '</p>'; }).join('<br/>'); }).join('<br/>'); 
 <div id="test"></div> 

You should loop through the data array not the Fields property: 您应该遍历数据数组而不是Fields属性:

  for(i = 0; i > data.length; i++){
   document.getElementById("test").innerHTML = data[i].fields
 .map(function(field) {
  return '<p>' + field.DisplayName + ': ' + field.DataValue + '</p>';
 })
 .join('\n');
 }

Based on the structure of the data the API is giving you, you could loop within a loop; 根据API提供给您的数据结构,您可以在一个循环中循环; eg: 例如:

 const data = [ {Fields: [{DisplayName: 'a', DataValue: 1}]}, {Fields: [{DisplayName: 'b', DataValue: 2}, {DisplayName: 'c', DataValue: 3}]}, ]; document.getElementById("test").innerHTML = data.map(({Fields}) => ( Fields.map(({DisplayName, DataValue}) => ( `<p>${DisplayName}: ${DataValue}</p>` )).join('\\n') )).join('\\n'); 
 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>title</title> </head> <body> <p id=test></p> </body> </html> 

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

相关问题 如何遍历 javascript 中的这个对象数组? - How can I loop through this array of objects in javascript? 如何遍历创建对象? - How can I loop through creating Objects? 如何遍历包含两个对象的数组 Alphavantage API - How to loop through an array of array of two objects Alphavantage API 如何使用“for循环”遍历对象数组并在模板文字中打印结果 - How can I loop through an array of objects using a 'for loop' and print the results in a template literal 如何循环遍历 json 数组项并使用某些 json 对象发出异步 API 请求 - How do I loop through an array item of json and make async API requests with certain json objects 如何在 O(n) 时间内循环使用包含数组的对象的数组? - How can I loop through an array with objects that contains an array in O(n) time? 从ajax json请求中,如何将对象动态添加到数组中,以便我可以遍历它们? - From an ajax json request, how can dynamically add the objects to an array so that I can loop through them? 如何在具有多个对象的数组中循环并仅使用Javascript列出某些元素? - How do I loop through an array with multiple objects and list certain elements in solely Javascript? 如何遍历对象数组中的数组 - How do I loop through an array in an array of objects 如何遍历对象数组 - How to loop through an array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM