javascript - 仅加载子数组的 3 个元素

标签 javascript

我正在用我的 javascript 对象创建一个 html 标签。

我只想添加 3 个具有属性 amzImgpartTitleimg 元素。

我尝试了以下方法:

const results = {
  "generalInfo": [{
    "post_id": 87,
    "title": "Test Title",
    "permalink": "http://localhost/test-title-4/",
    "category": [],
    "totalPrice": 331.99,
    "hardware": [{
        "partCategory": "x",
        "partTitle": "Test Title",
        "amzImg": "https://images-na.ssl-images.com/images/I/51wsdfgDLUEJwCL.jpg"
      },
      {
        "partCategory": "x",
        "partTitle": "Test Title",
        "amzImg": "https://images-na.ssl-images.com/images/I/51wsdfgDLUEJwCL.jpg"
      },
      {
        "partCategory": "x",
        "partTitle": "Test Title",
        "amzImg": "https://images-na.ssl-images.com/images/I/51wsdfgDLUEJwCL.jpg"
      },
      {
        "partCategory": "x",
        "partTitle": "Test Title",
        "amzImg": "https://images-na.ssl-images.com/images/I/51wsdfgDLUEJwCL.jpg"
      }
    ]
  }, {
    "post_id": 87,
    "title": "Test Title",
    "permalink": "http://localhost/test-title-4/",
    "category": [],
    "totalPrice": 331.99,
    "hardware": [{
        "partCategory": "x",
        "partTitle": "Test Title",
        "amzImg": "https://images-na.ssl-images.com/images/I/51wsdfgDLUEJwCL.jpg"
      },
      {
        "partCategory": "x",
        "partTitle": "Test Title",
        "amzImg": "https://images-na.ssl-images.com/images/I/51wsdfgDLUEJwCL.jpg"
      }
    ]
  }]
}

let dataSet = results.generalInfo.map((item, i) => [
  i + 1,
  `
    <img src="${item.hardware.amzImg}" alt="${item.hardware.partTitle}" height="42" width="42">
    <img src="${item.hardware.amzImg}" alt="${item.hardware.partTitle}" height="42" width="42">
    <img src="${item.hardware.amzImg}" alt="${item.hardware.partTitle}" height="42" width="42">
    <a href="${item.permalink}">
                    ${item.title}
                 </a>`,
  `$${item.totalPrice.toFixed(2)}`
])

console.log(dataSet)

但是,如果我的列表中只有 2 个元素,上面的示例会给出 3 个元素。对于第一个硬件对象,如下所示:

[ 1,'\n 
<img src="undefined" alt="undefined" height="42" width="42">\n    
<img src="undefined" alt="undefined" height="42" width="42">\n    
<img src="undefined" alt="undefined" height="42" width="42">\n
<a href="http://localhost/test-title-4/">\nTest Title\n</a>',
'$331.99'
],

对我做错了什么有什么建议吗?

我希望得到以下输出:

最佳答案

您需要在每次迭代中通过遍历 item.hardwares 手动构建数组。

const results = {
  "generalInfo": [{
    "post_id": 87,
    "title": "Test Title",
    "permalink": "http://localhost/test-title-4/",
    "category": [],
    "totalPrice": 331.99,
    "hardware": [{
        "partCategory": "x",
        "partTitle": "Test Title",
        "amzImg": "https://images-na.ssl-images.com/images/I/51wsdfgDLUEJwCL.jpg"
      },
      {
        "partCategory": "x",
        "partTitle": "Test Title",
        "amzImg": "https://images-na.ssl-images.com/images/I/51wsdfgDLUEJwCL.jpg"
      },
      {
        "partCategory": "x",
        "partTitle": "Test Title",
        "amzImg": "https://images-na.ssl-images.com/images/I/51wsdfgDLUEJwCL.jpg"
      },
      {
        "partCategory": "x",
        "partTitle": "Test Title",
        "amzImg": "https://images-na.ssl-images.com/images/I/51wsdfgDLUEJwCL.jpg"
      }
    ]
  }, {
    "post_id": 87,
    "title": "Test Title",
    "permalink": "http://localhost/test-title-4/",
    "category": [],
    "totalPrice": 331.99,
    "hardware": [{
        "partCategory": "x",
        "partTitle": "Test Title",
        "amzImg": "https://images-na.ssl-images.com/images/I/51wsdfgDLUEJwCL.jpg"
      },
      {
        "partCategory": "x",
        "partTitle": "Test Title",
        "amzImg": "https://images-na.ssl-images.com/images/I/51wsdfgDLUEJwCL.jpg"
      }
    ]
  }]
}

let dataSet = results.generalInfo.map((item, i) => {
  // build the array manually
  var data = [i + 1];
  var html = "";
  /*item.hardware.forEach((hardware) => {
      html += `<img src="${hardware.amzImg}" alt="${hardware.partTitle}">`;
  });*/
  
  for (var i = 0, len = item.hardware.length; i < len && i < 3; i++) {
     var hardware = item.hardware[i];
     html += `<img src="${hardware.amzImg}" alt="${hardware.partTitle}">`;
  }
  
  html += `<a href="${item.permalink}">${item.title}</a>`;
  data.push(html);
  data.push(`$${item.totalPrice.toFixed(2)}`);
  return data;
})

console.log(dataSet)

关于javascript - 仅加载子数组的 3 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49988664/

相关文章:

javascript - 查找 jQuery 元素的属性

javascript - 方法-重写 put 请求

javascript - 浏览器和nodejs上有没有可以将base64字符串转换为utf8字符串的API?

javascript - 函数未定义,但我已经定义了它

javascript - 在带有设置选择器的变量的添加函数中使用 'this'

javascript - 当我单击它时,没有使用 data-toggle=tab 检查单选按钮

javascript - 在 jQuery 中获取链接的原始值并更新

javascript - 页面上具有不同(线性和对数)轴的多个 d3 图表

javascript - lodash 的filterfirst 和filterlast 函数

javascript - 将对象推送到数组有时不起作用