微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 使用MomentJS的UnderscoreJS不会过滤动态数组的结果

我试图使用降序日期格式化数组的结果,但是当我尝试输入我的字符串receiveDataThree包含一个json时,我的函数不是我的数组而且在结果中只返回一个空数组但是如果我使用一个填充的数组使用静态数据,该功能可以毫无问题地工作吗?

$.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.js.
例如

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

这是一个JS小提琴:
https://jsfiddle.net/6rqe6hpg/1/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐