Array.find()获取正确的值但返回错误的值

时间:2016-03-30 20:46:23

标签: javascript arrays

所以,我认为编写一些Array方法来帮助排序和分组对象会很有趣。这是我的代码。请原谅log,这是在推动我的香蕉。您可以在JSBin here上运行此代码。

/**
 * uniquePush method - conditionally add an object to an array
 *
 * Only adds an object if it is not already present on this array.
 * Doesn't do a deep comparison.
 * 
 * @param {Object} newElement - the element to check for before pushing
 * @returns {Array} the array, modified if nescessary
 */
Array.prototype.uniquePush = function (newElement) {
  console.dir(this);
  var findCount = 0;

  function findNewElement(element) {
    console.log("findCount: " + (++findCount));
    console.log("Element: " + element);
    console.log("newElement: " + newElement);
    var found = (element === newElement);
    console.log("findNewElement -> " + found);
    return found;
  }

  var foundNewElement = this.find(findNewElement);
  console.log("this.find(findNewElement): " + foundNewElement)
  if (foundNewElement) {
    // Found it. Don't add it.
    console.log("Declined to add \'" + newElement + "\' to array");
  } else {
    // Didn't find it, go ahead and add it
    console.log("Adding \'" + newElement + "\' to array");
  }

  console.log("---------------------------------");
  return (foundNewElement ? this : this.push(newElement));
}

/**
 * groupBy method - groups an array of objects by property value
 *
 * Returned Array will look like:
 * [
 *  [{prop: "propValue", ...}, {prop: "propValue", ...}],
 *  [{prop: "anotherValue", ...}, {prop: "anotherValue", ...}],
 *  ...
 *  [{prop: "nValue", ...}, {prop: "nValue", ...}]
 * ]
 *
 * @param {String} prop - the property to group by
 * @returns {Array} an Array containing the grouped arrays
 */
Array.prototype.groupBy = function (prop) {
  // Gather all the possible property values
  var propValues = [];
  var entries = this.entries();

  var entryValue;
  while (entryValue = entries.next().value) {
    propValues.uniquePush(entryValue[1][prop]);
  }

  var result = [];
  for (var i = 0; i < propValues.length; i += 1) {
    var innerResult = [];
    this.forEach(function(element) {
      if (element[prop] === this.propValue) {
        this.result.push(element);
      }
    }, {
      propValue: propValues[i], 
      result: innerResult
    });
    result.push(innerResult);
  }
  return result;
};

var PRODUCTS = [
  {category: 'Sporting Goods', price: '$29.99', stocked: true, name: 'Football'},
  {category: 'Sporting Goods', price: '$9.99', stocked: true, name: 'Baseball'},
  {category: 'Sporting Goods', price: '$29.99', stocked: false, name: 'Basketball'},
  {category: 'Electronics', price: '$99.99', stocked: true, name: 'iPod Touch'},
  {category: 'Electronics', price: '$399.99', stocked: false, name: 'iPhone 5'},
  {category: 'Electronics', price: '$199.99', stocked: true, name: 'Nexus 7'}
];

var groupedByCategory = PRODUCTS.groupBy("category");
console.dir(groupedByCategory);
var groupedByStocked = PRODUCTS.groupBy("stocked");
console.dir(groupedByStocked);

这段代码大多数工作正常,但我发现它在布尔值数组上有问题。具体来说,uniquePush()方法使用Array.find()方法来确定newElement数组中是否存在给定的this

然而,它似乎正在返回错误的值。例如,当我在"stocked"上运行时,我得到了这个:

<edited for brevity>

[true, false]
"findCount: 1"
"Element: true"
"newElement: true"
"findNewElement -> true"
"foundNewElement: true"
"---------------------------------"
"Declined to add true to array"
[true, false]
"findCount: 1"
"Element: true"
"newElement: false"
"findNewElement -> false"
"findCount: 2"
"Element: false"
"newElement: false"
"findNewElement -> true"
"foundNewElement: false"
"---------------------------------"
"Adding false to array"
[true, false, false]

如您所见,最后,findNewElement评估为trueArray.find()返回false。有人可以向我解释为什么会这样吗?

3 个答案:

答案 0 :(得分:1)

如果找不到任何内容,

Array.find将返回undefined。

var foundNewElement = this.find(findNewElement);
  if (foundNewElement) ...

如果发现&#39; false&#39;这项检查失败了。使用if (typeof(foundElement)!='undefined')

答案 1 :(得分:1)

你的代码是混乱的方式。 这是一个您可能喜欢的简单解决方案。

Array.prototype.uniquePush = function (newElement) {
  return this.indexOf(newElement) !== -1 ? this : this.push(newElement);
}

https://jsbin.com/nexuboqavo/edit?html,output

答案 2 :(得分:1)

我认为你误解了[].find。它用于检索满足某些条件的第一个值,而不是检查值是否属于数组。

如果你想做后者,你可以使用