How to check whether multiple values exist within an Javascript array
function containsAll(needles, haystack){
for(var i = 0; i < needles.length; i++){
if($.inArray(needles[i], haystack) == -1) return false;
}
return true;
}
containsAll([34, 78, 89], [78, 67, 34, 99, 56, 89]); // true
containsAll([34, 78, 89], [78, 67, 99, 56, 89]); // false
containsAll([34, 78, 89], [78, 89]); // false
JavaScript - include() - A check to see if multiple elements are in an array
The issue is in your if statement because includes() returns a boolean based on the string parameter. A better way of doing this would be to use something like:
if(arr.includes("TL") && arr.includes("TM") && arr.includes("TR")) {
console.log("yes");
}
If you have lots of elements in your array I would suggest something more along the lines of:
var flag = true;
for(i=0; i<arr.length; i++) {
if(!arr.includes(arr[i])) {
flag = false;
}
}
if(flag) {
console.log("yes");
}
Array.include? multiple values
You could take the intersection of two arrays, and see if it's not empty:
([2, 6, 13, 99, 27] & [2, 6]).any?
multiple conditions for JavaScript .includes() method
That should work even if one, and only one of the conditions is true :
var str = "bonjour le monde vive le javascript";
var arr = ['bonjour','europe', 'c++'];
function contains(target, pattern){
var value = 0;
pattern.forEach(function(word){
value = value + target.includes(word);
});
return (value === 1)
}
console.log(contains(str, arr));
Multiple values inside array
Console.WriteLine(string.Join(",", array2.Reverse().Zip(array1, (a, b) => a*b)));
or
array3 = array2.Reverse().Zip(array1, (a, b) => a*b).ToArray();
check for multiple Array.includes using or
As the documentation the Array.includes
can only test for a single element.
You could reverse your logic though and use
if (['slug-title-one','slug-title-13'].some(slug => item.slug.includes(slug))) {
// checks for and displays if either
}
How to check if an array has multiple values and push to a new array if a value is met?
You can loop through the items and tags, store the items to the specific arrays and finally update the state. Check the code below-
const data = [
{
id: 'xyz',
name: 'test',
tags: ["Mechanical", "Director", "Specialist"]
},
{
id: 'abc',
name: 'abc',
tags: ["Mechanical", "Specialist"]
}
];
const _mechanical = [];
const _director = [];
const _specialist = [];
for (const item of data) {
if (item?.tags?.length > 0) {
for (const tag of item.tags) {
switch(tag.toLowerCase()) {
case 'mechanical':
_mechanical.push(item);
break;
case 'director':
_director.push(item);
break;
case 'specialist':
_specialist.push(item);
break;
}
}
}
}
console.log(JSON.stringify(_mechanical));
console.log(JSON.stringify(_director));
console.log(JSON.stringify(_specialist));
searching multiple values in string array in javascript/react
You can use every
and includes
to utilize your logic
const groups = ['A', 'B', 'C', 'D', 'E']
const userGroups = {
admin: ['A', 'B'],
researcher: ['C', 'E'],
riskManager: ['B', 'C', 'D']
}
function setAdmin() {
console.log('admin')
}
function setResearcher() {
console.log('researcher')
}
function setRiskMgr() {
console.log('risk manager')
}
if (userGroups.admin.every(group => groups.includes(group))) setAdmin(true);
if (userGroups.researcher.every(group => groups.includes(group))) setResearcher(true);
if (userGroups.riskManager.every(group => groups.includes(group))) setRiskMgr(true);
Related Topics
Ruby Easy Search for Key-Value Pair in an Array of Hashes
Rbenv Can't Change Global Ruby Version
Rails 3 Initializes Extremely Slow on Ruby 1.9.2
Sending a Post Request with Net/Http
Rendering Partials/View in a Rake Task/Background Job/Model in Rails 4
Where/How to Include Helper Methods for Capybara Integration Tests
Restarting Unicorn with Usr2 Doesn't Seem to Reload Production.Rb Settings
Ruby: Insert Spaces Every X Number of Characters
How to Do Basic Authentication Over Https in Ruby
Failed to Build JSON (1.6.3) with Native Extension After I Installed Xcode 4.2
What Are the Reserved Words Begin or End Used for in Ruby
Cucumber, Capybara and Selenium - Submitting a Form Without a Button
Puts Doesn't Print Stuff to Console