I wanted to ask, I have an array and I want to eliminate some elements in the array with elements that I have stored in an array, so the illustrations like this:

我想问一下,我有一个数组,我想用阵列中存储的元素消除数组中的一些元素,所以插图如下:

array1 = process, of, gathering, mature, crops, from, the, fields, Reaping, is, the, cutting array2 = of, from, the, is, a, an

array1 =进程,收集,成熟,庄稼,来自,领域,收割,是,切割array2 =,from,the,is,a,an

if there are elements in array1 is also an element of array2. then these elements will be eliminated.

如果array1中有元素也是array2的元素。然后这些元素将被淘汰。

the method I use like this:

我使用的方法如下:

var array1 = ["of","gathering","mature","crops","from","the","fields","Reaping","is","the","cutting"];
var kata = new Array();
kata[0] = " is ";
kata[1] = " the ";
kata[3] = " of ";
kata[4] = " a ";
kata[5] = " from ";


for(var i=0,regex; i<kata.length; i++){
        var regex = new RegExp(kata[i],"gi");
        array1 = array1.replace(regex," ");
    }

why I can not immediately eliminate the elements of array?

为什么我不能立即消除数组的元素?

I had been using the method: when I want to eliminate some elements that are in array1 then the array is my first change into a string by means of:

我一直在使用这个方法:当我想要消除array1中的一些元素时,数组是我第一次通过以下方式更改为字符串:

var kataptg = array1.join (" ");

however, if using that method there are several elements that should be lost but can be lost because the pattern did not like the array kata as above.

但是,如果使用该方法,有几个元素应该丢失,但可能会丢失,因为模式不像上面的数组kata。

suppose the word "of", the pattern of the array kata = "of"; but on the pattern array1 = "of";

假设单词“of”,数组的模式kata =“of”;但在模式array1 =“of”;

how do these elements can be removed even though the writing patterns differ from those in the array kata?

即使写入模式与数组kata中的写入模式不同,如何删除这些元素?

3 个解决方案

#1


0

# Simplified from
# http://phrogz.net/JS/Classes/ExtendingJavaScriptObjectsAndClasses.html#example5
Array.prototype.subtract=function(a2){ 
   var a1=this;
   for (var i=a1.length-1;i>=0;--i){ 
      for (var j=0,len=a2.length;j<len;j++) if (a2[j]==a1[i]) {
        a1.splice(i,1);
        break;
      } 
   } 
   return a1;
}

var a1 = "process of gathering mature crops from the fields".split(" ");
var a2 = "of from the is a an".split(" ");
a1.subtract(a2);
console.log(a1.join(' '));
// process gathering mature crops fields

If performance is an issue, there are clearly better ways that are not O(m*n), such as pushing the words from a2 into a object for constant-time lookup so that it's a linear-time pass through the source array to drop the ignored words, O(m+n):

如果性能是一个问题,那么显然有更好的方法不是O(m * n),例如将a2中的单词推入对象进行恒定时间查找,这样它就是线性时间通过源数组而下降忽略的单词,O(m + n):

var a1 = "process of gathering mature crops from the fields".split(" ");
var a2 = "of from the is a an".split(" ");
var ignores = {};
for (var i=a2.length-1;i>=0;--i) ignores[a2[i]] = true;
for (var i=a1.length-1;i>=0;--i) if (ignores[a1[i]]) a1.splice(i,1);
console.log(a1.join(' '));
// process gathering mature crops fields

Here's one more solution using regex (probably O(m+n)):

这是使用正则表达式的另一个解决方案(可能是O(m + n)):

var s1 = "process of gathering mature crops from the fields";
var a2 = "of from the is a an".split(" ");
var re = new RegExp( "\\b(?:"+a2.join("|")+")\\b\\s*", "gi" );
var s2 = s1.replace( re, '' );
console.log( re ); // /\b(?:of|from|the|is|a|an)\b/gi
console.log( s2 ); // "process gathering mature crops fields"

更多相关文章

  1. JavaScript循环输入创建一个对象数组
  2. 有没有办法检查两个数组是否具有相同的元素?
  3. 如何从json对象获取匹配元素的索引?
  4. 如何在Javascript中从Json数组创建路径路径?
  5. jQuery插件:如何将元素引用传递给回调函数?
  6. JavaScript数组操作函数方法详解
  7. 数组多重排序
  8. ES6学习笔记二之数组的扩展
  9. js的html元素的父节点,子节点

随机推荐

  1. c语言和c++区别大吗
  2. 用c语言编写爱心的代码是什么?
  3. C语言自定义函数(图文详解)
  4. c语言和vb语言的区别是什么?
  5. C语言中fputc函数的用法
  6. 小白程序员C++入门学习书籍(书单)
  7. c++中new的用法详解
  8. C程序的注释只能是一行吗?
  9. continue在C语言中什么意思?
  10. c++运算符重载的方法有哪些