0

I'd like to know how can I find strings inside one array similar to strings in another one using JavaScript,

I managed to find exact match with this:

var arr = ['English Question Answering', 'Questions And Reviews Organic Milk', 'Facebook Page Discovery (En) - 1426357'];
var keyword = ['English Question Answering', 'Facebook Page Discovery (En)']

var ret = []
arr.forEach(val => {
    if (keyword.includes(val[1])) {
        ret.push(`*${val[0]}* - ***${val[1]}*** - Pay: *${val[3]}* - tasks: *${val[5]}*`)
    }
})

How I do I make this work for approximate match, e.g. having:

var arr = ['English Question Answering', 'Questions And Reviews Organic Milk', 'Facebook Page Discovery (En) - 1426357'];
var keyword = ['English Question Answering', 'Facebook Page Discovery (En)'];

I expect to get:

['English Question Answering', 'Facebook Page Discovery (En) - 1426357'];
Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42
Victor Escalona
  • 545
  • 1
  • 6
  • 15
  • **Similar** is a vague concept. If you have specific rules that would identify something as similar then you could create a function that tests two items for similarities and use it in a `.find` instead of the `.includes` you use now. – Gabriele Petrioli Aug 30 '19 at 15:10
  • How do you define "similarity"? Whole academic papers have been written on the subject of linguistic similarity. In your example, all you'd need to do is `if (keyword.includes(val[1]) || val[1].includes(keyword)) { ...` (although I'm unclear on what exactly the indexes are in there for, since `val[1]` is "n", "u", or "a"... – Heretic Monkey Aug 30 '19 at 15:10
  • @HereticMonkey `keyword` is the other array so `val[1].includes(keyword)` would not work. – Gabriele Petrioli Aug 30 '19 at 15:11
  • @GabrielePetrioli Right, it'd have to be more like `keyword.some(kw => val[1].includes(kw))`, but the idea is that you search for each value of one array in each value of the other. – Heretic Monkey Aug 30 '19 at 15:37
  • Use fire.js look at url https://fusejs.io/ – dılo sürücü Aug 30 '19 at 15:46

2 Answers2

1

Check this question out: Compare Strings Javascript Return %of Likely

Basically, you have to write a function to compare two strings (there is an example function in the question linked above) and loop through the both arrays, comparing the strings one by one.

Some pseudocode:

function checkSimilarity(s1, s2) {
  // ... compare the strings s1 and s2
}

arrOne.forEach((s1) => {
    arrTwo.forEach((s2) => {
        if(checkSimilarity(s1, s2) > 0.5)
            console.log("Match found between" + s1 + " and " + s2);
            console.log(checkSimilarity(s1, s2));
        }
    });
});
TemporaryName
  • 487
  • 5
  • 16
-1

Assuming you have rules for what constitutes a similarity you could do

var ar = ['English Question Answering', 'Questions And Reviews Organic Milk', 'Facebook Page Discovery (En) - 1426357'];
var keyword = ['English Question Answering', 'Facebook Page Discovery (En)']

var ret = []

function similar(string1, string2) {
  return (string1 === string2) ||
    string1.includes(string2) ||
    string2.includes(string1)
}

ar.forEach(val => {
      if (keyword.some(word => similar(val, word))) {
          ret.push(`*${val[0]}* - ***${val[1]}*** - Pay: *${val[3]}* - tasks: *${val[5]}*`);
        }
      })
      
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317