使用$ .jquery.grep过滤后,从对象附加图像URL

时间:2018-01-07 00:34:50

标签: jquery arrays object

所以我尝试使用jquery来创建一个过滤器,我可以使用它来填充某些页面上的图像。

我设法让它过滤但是当我尝试控制日志时,结果显示为未定义。有什么想法吗?

$(document).ready(function() {

  var heroImage = [{}];

  var itemList = [{
      "itemImage ": "../images/trend/greenpants.jpg",
      "itemName": "Green Pants",
      "itemType": "pants",
      "itemClass": "trend",
      "itemDep": "male"
    },
    {
      "itemImage ": "../images/trend/greenpants.jpg",
      "itemName": "Snow Coat",
      "itemType": "coat",
      "itemClass": "collection",
      "itemDep": "female"
    },
    {
      "itemImage ": "../images/trend/greenpants.jpg",
      "itemName": "Orange Mittens",
      "itemType": "accessories",
      "itemClass": "trend"
    },
    {
      "itemImage ": "../images/trend/greenpants.jpg",
      "itemName": "White Blazer",
      "itemType": "shirt",
      "itemClass": "collection",
      "itemDep": "male"
    },
    {
      "itemImage ": "../images/trend/greenpants.jpg",
      "itemName": "Brown Shirt",
      "itemType": "shirt",
      "itemClass": "trend",
      "itemDep": "female"
    }

  ];

  /*$("div").text(JSON.stringify(itemList));*/

  var itemAarray = jQuery.grep(itemList, function(x, i) {
    return (x.itemDep === "male");
  }, false);

  console.log(itemAarray.itemName);

  var image = $('<img/>').attr('src', itemAarray.itemImage);

  $(".gallery").append(image);
  /*text(JSON.stringify(itemAarray))*/


});

1 个答案:

答案 0 :(得分:0)

对于那些好奇的人,我确实解决了这个问题!

我决定.grep不会工作,而是选择for循环。

这似乎解决了我的问题

Uncaught TypeError: $(...).datetimepicker is not a function
    at HTMLDocument.<anonymous>