javascript - 为什么这个函数不能处理这个特定的数组?

标签 javascript arrays

以下函数查找在数组中出现多次的单词对,然后将它们组合成单个数组元素。它适用于示例数组,但当我尝试将其实现到我的项目中(从抓取网页动态创建数组)时失败。

功能:

function combineCommon(arr) {
  var dictionary = {};
  for (var a = 0; a < arr.length - 1; a++) {
    var A = arr[a];
    if (dictionary[A] == void 0) {
      dictionary[A] = [];
    }
    dictionary[A].push(arr[a + 1]);
  }
  var res = [];
  for (var index = 0; index < arr.length; index++) {
    var element = arr[index];
    var pass = false;
    if (dictionary[element].length > 1) {
      if (dictionary[element]
        .some(function(a) {
          return a != dictionary[element][0];
        }) == false) {
        pass = true;
      }
    }
    if (pass) {
      res.push(arr[index] + " " + dictionary[element][0]);
      index++;
    } else {
      res.push(arr[index]);
    }
  }
  return res;
}
console.log(combineCommon(arr));

有效的数组:

var arr = ["john", "smith", "says", "that", "a", "lock", "smith", "can", "open", "the", "lock", "unlike", "john", "smith"];

无效的数组:

var arr = ['Social', 'care', 'fund', 'fails', 'to', 'reduce', 'pressure', 'on', 'hospital', 'beds', 'Court', 'questions', 'whether', 'US', 'travel', 'ban', 'is', 'anti', 'Muslim', 'Police', 'pay', 'out', 'at', 'least', '£195m', 'to', 'informants', 'in', 'five', 'years', 'Brexit', 'rebellion', 'avoided', 'after', 'meaningful', 'vote', 'offer', 'FA', 'reforms', 'Chairman', 'Greg', 'Clarke', 'to', 'quit', 'if', 'government', 'does', 'not', 'back', 'plans', 'Louisiana', 'tornadoes', 'The', 'whole', 'house', 'fell', 'apart', 'Uncertainty', 'over', '30', 'hours', 'free', 'childcare', 'say', 'councils', 'Uncertainty', 'over', '30', 'hours', 'free', 'childcare', 'say', 'councils', 'Hans', 'Rosling', 'Data', 'visionary', 'and', 'educator', 'dies', 'aged', '68', 'Dakota', 'Access', 'Pipeline', 'to', 'win', 'US', 'Army', 'permit', 'for', 'completion']

这是一个jsfiddle展示。为什么第二个数组不起作用?

最佳答案

构建字典时似乎存在一个错误,这导致数组的最后一个单词未添加到字典中。第一个示例之所以有效,是因为最后一个单词(“smith”)之前已包含在数组中。

第三行应该是for (var a = 0; a < arr.length; a++) { ,即:

function combineCommon(arr) {
 var dictionary = {};
  for (var a = 0; a < arr.length; a++) {
    var A = arr[a];
    if (dictionary[A] == void 0) {
      dictionary[A] = [];
    }
    dictionary[A].push(arr[a + 1]);
  }
  var res = [];
  for (var index = 0; index < arr.length; index++) {
    var element = arr[index];
    var pass = false;
    if (dictionary[element].length > 1) {
      if (dictionary[element]
        .some(function(a) {
          return a != dictionary[element][0];
        }) == false) {
        pass = true;
      }
    }
    if (pass) {
      res.push(arr[index] + " " + dictionary[element][0]);
      index++;
    } else {
      res.push(arr[index]);
    }
  }
  return res;
}
console.log(combineCommon(arr));

关于javascript - 为什么这个函数不能处理这个特定的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42105129/

相关文章:

c - 在 C 编程中使用数组、循环和条件

javascript - Mongoose MongoDB : LIKE operator in search

javascript - AngularJS 检查图像是否有 404 并获取图像 src 的动态 URL

javascript - 尝试使用数据库显示图像 HTML

arrays - Julia 矢量 : Excessive Memory Usage

php - 合并多个对象

javascript - 查找多个变量的值 - JS

javascript - 如何使用 className 在一次调用中 pop() 2 个数组的最后一个元素?

java - '.class' 预期错误 java 和数组

arrays - 如何找到数组中出现次数超过 n/k 次的所有值?