有没有办法从数组的元素中删除相似的单词?

时间:2017-12-16 18:38:18

标签: javascript

有没有办法从数组元素中删除相似的单词?

初始数组:

var arr = ["element first", "element second", "element third"];

期望的结果:

var result = ["first", "second", "third"];

谢谢!

5 个答案:

答案 0 :(得分:2)

您可以找到常用字符(在同一索引处)并过滤字符串的字符。

var array = ["element first", "element second", "element third"],
    common = array
        .map(a => [...a])
        .reduce((a, b) => a.map((v, i) => v === b[i] ? v : null))
        .filter(Boolean),
    result = array.map(a => [...a].filter((v, i) => v !== common[i]).join(''));
    
console.log(result);
console.log(common);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

您可以删除所有字符串中的第一个单词。

var arr = ["element first", "element second", "element third"];

var output = []; 
for (var i =0; i < arr.length; i++) {
    var items =  arr[i].split(" ");
    arr[i] = arr[i].replace(items[0] + " ", "");
    output[i] = arr[i];
}

console.log(output);

输出是您想要的数组。

答案 2 :(得分:0)

注意:使用ES6。

&#13;
&#13;
const arr = ["element first", "element second", "element third"];

const replaced = arr.map(el => el.replace('element ',''));

console.log(replaced);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您可以创建arr数组中包含的单词的字典,并为字典中的每个条目分配索引。这就是:

&#13;
&#13;
var wordList = ["element first", "element second", "element third"]
// this could be a object, but im going with a Map
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
var wordMap = new Map()

for(var i = 0; i < wordList.length; i++) {
  var words = wordList[i].split(/\s/)
  // go trough every (possible) word
  for(var j = 0; j < words.length; j++) {
    var word = words[j]
    // check if the map already contains the word
    if(!wordMap.has(word)) {
      // the map doesn't contain the word, add it
      wordMap.set(word, [i])
    } else {
      // the map already contains the word, get the current indexes
      var override = wordMap.get(word)
      // add the current index to the list
      override.push(i)
      // update the map entry
      wordMap.set(word, override)
    }
  }
}

var mapEntries = wordMap.entries()
var iterator = mapEntries.next()
// go trough every map entry
while(!iterator.done) {
  var word = iterator.value[0]
  var indexes = iterator.value[1]
  // there must be atleast one index
  if(indexes.length !== 1) {
    // there are multiple indexes
    for(var k = 0; k < indexes.length; k++) {
      // remove the word and assign the new string to the position of the old string
      // the RegExp and the String.trim functions are used to remove unnecessary whitespace
      wordList[k] = wordList[k].replace(new RegExp(word + '\\s?'), '').trim()
    }
  }
  // get the next entry pair
  iterator = mapEntries.next()
}

console.log(wordList)
&#13;
&#13;
&#13;

答案 4 :(得分:0)

  • 使用正确的尾递归函数将数组减少到其公共前缀,以建立与当前字符串的开头匹配的当前前缀的子集
  • 将数组映射到一个新的字符串数组,这些字符串是超出前缀长度的字符切片。

&#13;
&#13;
const arr = ["element first", "element second", "element third"],
  pre = arr.reduce((pfx, b) => toPrefix(pfx, b)),
  res = arr.map(a => a.slice(pre.length));

console.log("removed: %s\nresult: %s", JSON.stringify(pre), JSON.stringify(res));

function toPrefix(pfx, b, n = 0) {
  return b[n] && pfx[n] === b[n] ? toPrefix(pfx, b, n+1) : b.slice(0, n);
}
&#13;
&#13;
&#13;