'Compare select parts of 2 Strings
0: {Id: 1, type: dog, changeNo: '0123-123-A1', …}
1: {Id: 2, type: cat, changeNo: '0123-123-F2', …}
2: {Id: 3, type: bus, changeNo: '0456-154-E5', …}
n: {Id: n, type: n, changeNo: 'n', …}
length: n
So I'm currently learning JS and react and am trying to check an object array in the form above for the same changeNo up to the second '-'. So I'd like to know if, and how many '0123-123' there are. I figured I'd iterate over the array checking for whether either string is a subset of the other, however that would lead me into errors with almost everything as it checks the adjacent Lettering as well.
let multi: number;
for (let i=0; i <= data.length; i++) {
for (let j=1; j <= data.length; j++) {
while (data[i].changeNo.indexOf(data[j].changeNo) >-1) {
multi ++;
}
}
}
return multi-1;
Is there a way to easily compare the strings in a for while loop based on the first few elements. Or maybe a way to stop comparing once the second '-' is reached?
Thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
