简体   繁体   English

循环遍历对象 Javascript 的两个 arrays

[英]Loop through two arrays of objects Javascript

so I want to load multiple cards into 1 div box, some with same css classes, some different, so I create one array of objects to load these different classes into them.所以我想将多张卡加载到 1 个 div 框中,有些具有相同的 css 类,有些不同,所以我创建了一个对象数组来将这些不同的类加载到其中。 In the mean time, I have other array of object including data to load too.同时,我还有其他的 object 数组,包括要加载的数据。

What I'm trying here is loops these 2 arrays in 1 for loop statement (same length by using same iterator) but one of them returned with undefined, can someone help me please, what am I doing wrong?我在这里尝试的是在 1 个 for 循环语句中循环这 2 个 arrays(使用相同的迭代器,长度相同),但其中一个返回未定义,有人可以帮我吗,我做错了什么? (The code below is just a simple version of mine because the real one is really long) (下面的代码只是我的一个简单版本,因为真正的代码真的很长)

<div id="recently-added" class="u-repeater">
// here to load multiple cards
</div>
var DB = [];

$.get(`http://localhost:8080/real-estate/monthly`, function(res) {
    DB.push(res);
}) 

load();

function load() {
    var card = '';
    var classNum = [
        { 'list': '1', 'text': '2'},
        { 'list': '2', 'text': '5'}
    ];
    for (let index = 0; index < classNum.length; index++) {
         var data = DB[index];
         card += 
            `
             <div class="u-list-item-${classNum[index].list}">
              <div class="u-container-layout-${classNum[index].list}">
              <img class="u-image-${classNum[index].list}">
              <p class="u-text">Click to select the text box</p>
              <h4 u-text-${classNum[index].text}">${data.title}</h4>
              <a class="u-btn-${classNum[index].list}">detail</a>
              </div>
            </div>
           `    
        }
    $("#recently-added").html(card);
}

The DB I took from ajax call and it somehome looks like this:我从 ajax 调用中获取的数据库看起来像这样:

var DB = [
 {'id': 1350, 'title': '2-room apartment with pool view', 'type': 0, 'request': 0},
 {'id': 1351, 'title': 'newly built house', 'type': 0, 'request': 1}
];

As I watched them through console, the data variable returned undefined https://imgur.com/a/6ZlWc4C当我通过控制台查看它们时, data变量返回未定义的https://imgur.com/a/6ZlWc4C

This is kind of the result I expect: https://imgur.com/a/ttkh17I这是我期望的结果: https://imgur.com/a/ttkh17I

$.get(`http://localhost:8080/real-estate/monthly`, function(res) {
    
    load(res.data);     // or however your get call returns the atual array you want
}) 



function load(DB) {
var card = '';
var classNum = [
    { 'list': '1', 'text': '2'},
    { 'list': '2', 'text': '5'}
];

// note that now you are checking two arrays using the length of only one array
// and you dont control the other array, 
//so this will likely cause you problems if only 1 element is returned from 
// the get call

for (let index = 0; index < classNum.length; index++) {

     var data = DB[index];
     card += 
        `
         <div class="u-list-item-${classNum[index].list}">
          <div class="u-container-layout-${classNum[index].list}">
          <img class="u-image-${classNum[index].list}">
          <p class="u-text">Click to select the text box</p>
          <h4 u-text-${classNum[index].text}">${data.title}</h4>
          <a class="u-btn-${classNum[index].list}">detail</a>
          </div>
        </div>
       `    
    }
$("#recently-added").html(card);

} }

Try this one - call load() only after fetching results:试试这个 - 仅在获取结果后调用load()

$.get(`http://localhost:8080/real-estate/monthly`, function(res) {
    DB.push(res);
    load();
}) 

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

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