Check if certain stings (even with spaces) are in array
Check if certain stings (even with spaces) are in array
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');
}
4 Answers
4
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.
attr("content")
split(',')
map
trim()
You can use some()
to loop thru the myArray
array and use includes
to check whether an array includes a certain string.
some()
myArray
includes
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);
https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
<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()
filter()
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);
https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
<meta name="keywords" content="dog, cat, humming bird, shark, king cobra">
//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!');
}
});
https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
<meta name="keywords" content="dog, cat, humming bird, shark, king cobra">
Change
var myArray = $("meta[name='keywords']").attr("content");
to
var myArray = $("meta[name='keywords']").attr("content").split(", ");
Then your code should work.
It still won't work.
$.inArray()
doesn't compare two arrays.– Barmar
Jun 29 at 18:25
$.inArray()
After using .split(', ')
on your keywords
to create an array,
you could use a function to get all the similar elements:
.split(', ')
keywords
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);
https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
<meta name="keywords" content="dog, cat, humming bird, shark, king cobra">
Hope it helps.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of Check if an array contains any element of another array in JavaScript
– Mike McCaughan
Jun 29 at 17:50