Reputation: 55
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 " ];
i.e 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];
}
}
}
}
i.e 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
Upvotes: 2
Views: 254
Reputation: 644
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 :)
Upvotes: 1
Reputation: 386520
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);
Upvotes: 3
Reputation: 349956
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);
Upvotes: 2
Reputation: 7360
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)
Upvotes: 1