简体   繁体   中英

why i can not remove elements of an array?

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

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

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:

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.

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

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

The items in array1 don't have quotes around them, so JavaScript thinks they're (undefined) variables.

Assuming you've fixed that (and the stray quotes), your next problem is you're calling replace() on an array object. replace() only works on strings.

# 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):

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)):

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"

You can put all the 'discards' in a single reg exp and test each array item for any of them.

By starting at the end of the array you can splice out any discards as you progress towards the start of the array.

var array1= ['of','gathering','mature','crops','from','the','fields',
'Reaping','is','the','cutting'],
kata= ['is','the','of','a','from'], L= array1.length,
rx= RegExp('^('+kata.join('|')+')$','i');

while(L){
    if(rx.test(array1[--L])) array1.splice(L, 1);
}

alert(array1)

/* returned value: (Array) ['gathering', 'mature', 'crops', 'fields', 'Reaping', 'cutting'] */

(The rx here is= /^(is|the|of|a|from)$/i)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM