2

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

2
  • 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]; } } } } This is my code. But i cant able to get the result Commented Jul 9, 2016 at 11:47
  • @Sahithya You need to put the code (that you have tried) in the original question as well. Also, it is not clear what you intend to do. Do you want the final result to have unique prefixes? In that case all your items have same prefix as proj, shouldn't that ensure that all other items be removed? Please share enough test data with intended output for each test case Commented Jul 9, 2016 at 11:50

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't work if same elements are in a different order, like ` var res1 = [" Proj -> Phase-2 ", " Proj -> Phase-1 -> Tower-1 -> Floor-2 ", " Proj -> Phase-2 -> Tower-1 "]`
2

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

Comments

1

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)

1 Comment

Thank you. This is what i need exactly. :)
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.