JavaScript函数Array.prototype.includes()不';我不能在循环中工作

JavaScript函数Array.prototype.includes()不';我不能在循环中工作,javascript,jquery,arrays,javascript-objects,Javascript,Jquery,Arrays,Javascript Objects,我有一个web应用程序,可以跟踪您的购买并显示不同的统计数据。在其中一个页面中,我有jqueryajax请求,通过API调用加载用户的购买。然后,所有购买都作为JavaScript对象放入一个全局数组,称为G_purchases 到目前为止还不错。然后我调用一个函数,该函数使用jQuery的Deferred()使其可链接;它迭代G_PURCHASES,并通过检查G_PURCHASES[i].item.category是否包含在另一个名为G_CATEGORIES的全局数组中,使用array.inc

我有一个web应用程序,可以跟踪您的购买并显示不同的统计数据。在其中一个页面中,我有jqueryajax请求,通过API调用加载用户的购买。然后,所有购买都作为JavaScript对象放入一个全局数组,称为
G_purchases

到目前为止还不错。然后我调用一个函数,该函数使用jQuery的
Deferred()
使其可链接;它迭代
G_PURCHASES
,并通过检查
G_PURCHASES[i].item.category
是否包含在另一个名为
G_CATEGORIES
的全局数组中,使用
array.includes()
来获取所有不同的
purchase.item.category
(查看采购对象的相关结构)。如果不是,则
将其推入
G_类别中

我遇到的错误是,即使将
category
对象推入
G_CATEGORIES
数组中,
array.includes()
检查每次仍返回
false
。检查相关代码并输出以清除它

// Relevant structure of the purchase object
purchase = {
  item: {
    category: {
      categoryID: int,
      name: string
    }
}


我尝试使用
Array.indexOf()
和jQuery的
$.inArray()
,但没有成功。无论我如何
console.log()
我似乎都找不到错误所在。所以,如果你能告诉我为什么
Array.includes()
for
循环中不起作用,我会找到你,给你买杯啤酒

Well包括对引用相等性的检查,因此可能有两个具有相同属性和值的对象,但它们仍然是不同的对象,因此它们的引用不相等。您可能希望手动检查每个类别对象的categoryID和名称以查找重复项。

为什么不执行
console.log(allC,allP[i])
并查看您得到了什么。我得到了一个数组和一个对象,正如预期的那样:
Array[]object{item:object}
Array[object]object{item:object}
数组[Object,Object]Object{item:Object}
等等一个简单的二次循环迭代Categories数组并检查categoryID匹配修复了所有问题!谢谢!
// Relevant code

var G_PURCHASES = []; // array declared globally
// it is successfully filled with @purchase objects from another function

var G_CATEGORIES = []; // array declared globally
// to be filled by @LoadAllCategories

var LoadAllCategories = function() {

  // make a jQuery Deffered
  let func = $.Deferred(function() {

    let allP = G_PURCHASES; // make a shortcut
    let allC = C_CATEGORIES; // make another shortcut
    for (var i = 0; i < allP.length; i++) {

      // get whether the current purchase.item.category
      // already exists in allC array
      let exist = allC.includes(allP[i].item.category);

      // console.log the above result
      console.log('i = ' + i + ', category exists = ' + exist);

      // if it doesn't exist then push it in
      if (!exist) allC.push(allP[i].item.category);
    }

    this.resolve();

  });

  return func;
}
// Input
G_PURCHASES has 6 @purchase objects with 3 unique item.category 'ies

// Output
i = 0, category exists = false
i = 1, category exists = false
i = 2, category exists = false
i = 3, category exists = false
i = 4, category exists = false
i = 5, category exists = false

// Result
G_CATEGORIES contains duplicate categories