简体   繁体   中英

Remove a specific element in an array

My need is to remove an element from an array when an array element partially matches with some other element of a string, remove that element.

For example,

var res1 = [
  " Proj -> Phase-1 -> Tower-1 -> Floor-2 ", 
  " Proj -> Phase-2 ", 
  " Proj -> Phase-2 -> Tower-1 " ];

ie if my

res1[2]="Proj->Phase-2->Tower-1" 

and

res1[1]="Proj->Phase-2"

res1[1] partially matches with res1[2] . So i need to remove res1[2] . After that i want the remaining array values. But its not working properly.

Here's what I've tried, but I'm not getting the result I expect:

for (i = 0; i < res1.length; i++) {
    for (j = 0; j < res1.length; j++) {
        if (res1[i] != res1[j]) {
            if (res1.indexOf(res1[j]) === 0) {
                console.log(res1);
                console.log(res1[j]);
                delete res1[i];
            }
        }
    }
}

ie if string1 "Proj->Phase-2" exactly in string2 "Proj->Phase-2->Tower-1" , then string2 need to be deleted. Even if a string3 "Proj->Phase-1->Tower-1" compared with string1, it is not exactly the same. So it should not be removed

You were almost there. Just check if the indices are not the same

i !== j

and then if one string is in the other

res1[i].indexOf(res1[j]) === 0
//  ^^^

then use Array#splice for deleting the element.

And while the foor loop increments by one and the actual element of the outer loop is deleted, you need a correction for the index i . Then a break is necessary, because of the changed structure and need to initialize a new loop with j .

 var res1 = [" Proj -> Phase-1 -> Tower-1 -> Floor-2 ", " Proj -> Phase-2 ", " Proj -> Phase-2 -> Tower-1 "], i, j; for (i = 0; i < res1.length; i++) { for (j = 0; j < res1.length; j++) { if (i !== j && res1[i].indexOf(res1[j]) === 0) { res1.splice(i, 1); i--; break; } } } console.log(res1); 

A solution with EcmaScript2015:

 var res1 = [ " Proj -> Phase-1 -> Tower-1 -> Floor-2 ", " Proj -> Phase-2 ", " Proj -> Phase-2 -> Tower-1 " ]; res1 = res1.filter((x, i) => res1.every((y, j) => i==j || x.indexOf(y))); console.log(res1); 

Or the same without arrow functions:

 var res1 = [ " Proj -> Phase-1 -> Tower-1 -> Floor-2 ", " Proj -> Phase-2 ", " Proj -> Phase-2 -> Tower-1 " ]; res1 = res1.filter(function(x, i) { return res1.every(function(y, j) { return i==j || x.indexOf(y); }) }); console.log(res1); 

I would use String.includes()

 var res1 = [ " Proj -> Phase-1 -> Tower-1 -> Floor-2 ", " Proj -> Phase-2 ", " Proj -> Phase-2 -> Tower-1 " ]; for (var i = 0; i < res1.length; i++) { for (var j = i + 1; j < res1.length; j++) { if (res1[j].includes(res1[i])) { res1.splice(j, 1); j--; // Splice deletes the element so all index shifts, need to recheck the same element } } } console.log(res1) 

I'm not sure if this is the result you needed but here you go, the commans you must have used in the if construct was:

res1[j].indexOf(res1[i])==0

so the entire program is:

for(i in res1){
  for(j in res1){
    if(i!=j){
      if(res1[j].indexOf(res1[i])==0){
       delete res1[j];
      }
    }
  }
}
alert(res1);

Hope this helps :)

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