var groupArray = [
['10', 'group one result', 'more result from group1'],
['10 20 30', 'group two result', 'another result from group two'],
['10 20 40', 'group three result']
];
function compare(entryArr, stringArr) {
for (var i in entryArr) {
str = stringArr.split(' '); //to array
ent = entryArr[i][0].split(' '); //first items of each group to array
if (str.every(s => ent.includes(s))) // compare
return entryArr[i].slice(1);
}
}
//find match from possible strings
console.log(compare(groupArray, '10')) // true
console.log(compare(groupArray, '10 20')) // true
console.log(compare(groupArray, '10 20 60')) // undefined. the result should come from group two
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Above is the structure of my project.
I need to return the most relevant result when comparing string and entry arrays.
Array.prototype.every() Is the closest I've got in achieving my goal after countless hours trial and error of trying...
The problem is shown in output #3. I understand Array.prototype.every() will return false if the element to find is not present in the array but there must be a workaround.
If I remove non similar elements on both arrays, I thought it would solve the problem.
xyz = str.filter(x => ent.includes(x)) //output: ['10','20'] for sample #3
I try to use xyz as the new array for strings but then the element in entry array will still be present, thus will return false.
I can get the difference on both arrays...
entArr = str.filter(x => !ent.includes(x)) // entry array difference
strArr = ent.filter(x => !str.includes(x)) // string array difference
...but then I'm stuck in trying to find the suitable condition to apply.
Hope I explained it well and thanks for your help..