带有 MomentJS 的 UnderscoreJS 不会过滤动态数组的结果

UnderscoreJS with MomentJS does not filter the results of a dynamic array

我正在尝试使用降序日期格式化数组的结果,但是当我尝试输入包含 json 的字符串 receivedDataThree 时,我的函数并没有在我的数组中结果 returns 只是一个空数组,但如果我使用带有静态数据的填充数组,该函数可以正常工作,可能是什么原因造成的?

$.ajax({
  type: "GET",
  dataType: "JSON",
  url: 'https://famagas-api.herokuapp.com/api/itapeCompanyNews',
  success: function(allResponseThree) {
    // Native Array
    var receivedDataThree = [];

    // Group Received JSON By Total Of Purchased
    for (var i = 0; i < allResponseThree.length; i++) {
      // Group Received JSON By Total Of Purchased
      var totalOfPurchased = allResponseThree[i].purchase_history;
      for (var x = 0; x < totalOfPurchased.length; x++) {
        // Declare Variables Containing Extend Details
        var purchasedProductNested = totalOfPurchased[x].purchasedProduct;
        var dateOfPurchased = totalOfPurchased[x].dateOfPurchased;
        var quantityOfPurchasedNested = totalOfPurchased[x].quantityPurchased;
        var totalOfPurchasedNested = totalOfPurchased[x].totalOfPurchased;
        var totalOfDiscountsPurchasedNested = totalOfPurchased[x].discountsToConsider;

        // Format The Money So That It Is Possible To Count
        totalOfPurchasedNested = totalOfPurchasedNested.replace(/\./g, "").replace(",", ".");
        totalOfDiscountsPurchasedNested = totalOfDiscountsPurchasedNested.replace(/\./g, "").replace(",", ".");

        // Final JSON Array Containing Extend Details
        var allResponseThreeWithExtendDetails = {
          product: purchasedProductNested,
          dateOfPurchased: dateOfPurchased,
          quantity: JSON.parse(quantityOfPurchasedNested),
          totalPurchased: JSON.parse(totalOfPurchasedNested),
          totalDiscounts: JSON.parse(totalOfDiscountsPurchasedNested)
        };

        // Push Group With Received JSON In Native Array
        receivedDataThree.push(allResponseThreeWithExtendDetails);
      }
    }

    // Set Locale For Moment JS
    moment.locale("pt-BR");

    // Get Today Date
    var todayDate = moment();
    todayDate = todayDate.subtract(0, "days");
    todayDate = todayDate.format("DD/MM/YYYY");

    // Filter Purchased By Date
    var timeCreated = '11/05/2018';
    var arrayOfObjects = [{
        date: "11/04/2018",
        name: "Michael"
      },
      {
        date: "11/04/2018",
        name: "Larry"
      },
      {
        date: "11/12/2014",
        name: "Dean"
      },
      {
        date: "03/01/2015",
        name: "Jennifer"
      }
    ];

    var sortDate = _.filter(arrayOfObjects, function(desc) {
      return moment(desc.date).isAfter(moment(todayDate).subtract(30, 'days'))
    });

    console.log(sortDate);
  });
})

更详细地解释,当我尝试在我的数据过滤函数中声明我的数组 receivedDataThree 和字段 dateOfPurchased

var sortDate = _.filter(receivedDataThree, function(desc) {
  return moment(desc.dateOfPurchased).isAfter(moment(todayDate).subtract(30, 'days'))
});

只有这个还给我:

elements.js:449 
[]

但是,如果我将函数中的数组条目移动到静态数组,结果是有效的并且可以正常工作..

var arrayOfObjects = [{
    date: "11/04/2018",
    name: "Michael"
  },
  {
    date: "11/04/2018",
    name: "Larry"
  },
  {
    date: "11/12/2014",
    name: "Dean"
  },
  {
    date: "03/01/2015",
    name: "Jennifer"
  }
];

var sortDate = _.filter(arrayOfObjects, function(desc) {
  return moment(desc.date).isAfter(moment(todayDate).subtract(30, 'days'))
});

console.log(sortDate);

静态数组的结果

(2) [{…}, {…}]
{date: "11/04/2018", name: "Michael"}
{date: "11/04/2018", name: "Larry"}

可以帮我吗?我认为将数据从我的 ajax 响应推送到数组是错误的,但事实并非如此,所以我完全不知道如何解决这个问题,我将非常感谢任何帮助我的人......

谢谢;D

我相信这应该可以解决问题,我认为主要问题是没有将日期格式传递给 moment.js 构造函数。

$.ajax({
  type: "GET",
  dataType: "JSON",
  url: 'https://famagas-api.herokuapp.com/api/itapeCompanyNews',
  success: function(allResponseThree) {
    // Native Array
    var receivedDataThree = [];

    // Group Received JSON By Total Of Purchased
    for (var i = 0; i < allResponseThree.length; i++) {
      // Group Received JSON By Total Of Purchased
      var totalOfPurchased = allResponseThree[i].purchase_history;
      for (var x = 0; x < totalOfPurchased.length; x++) {
        // Declare Variables Containing Extend Details
        var purchasedProductNested = totalOfPurchased[x].purchasedProduct;
        var dateOfPurchased = totalOfPurchased[x].dateOfPurchased;
        var quantityOfPurchasedNested = totalOfPurchased[x].quantityPurchased;
        var totalOfPurchasedNested = totalOfPurchased[x].totalOfPurchased;
        var totalOfDiscountsPurchasedNested = totalOfPurchased[x].discountsToConsider;

        // Format The Money So That It Is Possible To Count
        totalOfPurchasedNested = totalOfPurchasedNested.replace(/\./g, "").replace(",", ".");
        totalOfDiscountsPurchasedNested = totalOfDiscountsPurchasedNested.replace(/\./g, "").replace(",", ".");

        // Final JSON Array Containing Extend Details
        var allResponseThreeWithExtendDetails = {
          product: purchasedProductNested,
          dateOfPurchased: dateOfPurchased,
          quantity: JSON.parse(quantityOfPurchasedNested),
          totalPurchased: JSON.parse(totalOfPurchasedNested),
          totalDiscounts: JSON.parse(totalOfDiscountsPurchasedNested)
        };

        // Push Group With Received JSON In Native Array
        receivedDataThree.push(allResponseThreeWithExtendDetails);
      }
    }

    // Set Locale For Moment JS
    moment.locale("pt-BR");
    var todayDate = moment();

    var sortDate = _.filter(receivedDataThree, function(desc) {
      return moment(desc.dateOfPurchased, "DD/MM/YYYY").isAfter(todayDate.subtract(30, 'days'))
    });

    console.log('Original data: ', receivedDataThree);
    console.log('Filtered data: ', sortDate);
  }
});

这只是我更改的最后几行。主要区别在于我们在 moment(..) 构造函数中将日期格式传递给 moment.js 。 例如

moment(desc.dateOfPurchased, "DD/MM/YYYY");

这是一个 JS Fiddle: https://jsfiddle.net/6rqe6hpg/1/