0

Trying to compare two arrays and see if existing words (even with spaces) exists in both. See below.

I think I have a rough idea. Thanks ahead for your help!

HTML

<meta name="keywords" content="dog, cat, humming bird, shark, king cobra">

Jquery

var myArray = $("meta[name='keywords']").attr("content");
var myAnimals = ["humming bird","king cobra"];
if (jQuery.inArray(myAnimals, myArray) != -1) {
    alert('animal exists');
} 
Barmar
  • 741,623
  • 53
  • 500
  • 612
user2428993
  • 323
  • 1
  • 10
  • Possible duplicate of [Check if an array contains any element of another array in JavaScript](https://stackoverflow.com/questions/16312528/check-if-an-array-contains-any-element-of-another-array-in-javascript) – Heretic Monkey Jun 29 '18 at 17:50

4 Answers4

2

attr("content") will return a string. You can use split(',') to convert the string into an array. Use map to loop thru the array and trim() the spaces.

You can use some() to loop thru the myArray array and use includes to check whether an array includes a certain string.

var myArray = $("meta[name='keywords']").attr("content").split(',').map(o => o.trim());
var myAnimals = ["humming bird", "king cobra"];

var result = myArray.some(o => myAnimals.includes(o));
console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<meta name="keywords" content="dog, cat, humming bird, shark, king cobra">

If you want to get the actual array elements, you can use filter() and includes()

var myArray = $("meta[name='keywords']").attr("content").split(',').map(o => o.trim());
var myAnimals = ["humming bird", "king cobra", "owl"];

var result = myArray.filter(o => myAnimals.includes(o));

console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<meta name="keywords" content="dog, cat, humming bird, shark, king cobra">
Eddie
  • 26,593
  • 6
  • 36
  • 58
1

//split the keywords so it is a real array
var myArray = $("meta[name='keywords']").attr("content").split(', ');
console.log(myArray);

var myAnimals = ["humming bird","king cobra"];

//loop over your animals
myAnimals.forEach(function(animal){
  //check if each animal is in the array
  if (myArray.indexOf(animal) > -1) {
    console.log(animal +' exists!');
  } else {
    console.log(animal +' does not exist!');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<meta name="keywords" content="dog, cat, humming bird, shark, king cobra">
Taplar
  • 24,788
  • 4
  • 22
  • 35
0

Change

var myArray = $("meta[name='keywords']").attr("content");

to

var myArray = $("meta[name='keywords']").attr("content").split(", ");

Then your code should work.

Rumple
  • 198
  • 1
  • 9
0

After using .split(', ') on your keywords to create an array,
you could use a function to get all the similar elements:

var myArray = $("meta[name='keywords']").attr("content").split(', ');
var myAnimals = ["humming bird", "king cobra"];

function get_similarities(arrayA, arrayB) {
  var matches = [];
  for (var i = 0; i < arrayA.length; i++) {
    if (arrayB.includes(arrayA[i]))
      matches.push(arrayA[i]);
  }
  return matches;
}

var similarities = get_similarities(myArray, myAnimals);
console.log('Found in both arrays:', similarities);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<meta name="keywords" content="dog, cat, humming bird, shark, king cobra">

Hope it helps.

Takit Isy
  • 9,688
  • 3
  • 23
  • 47