How to Check If an Array Includes a Value in JavaScript

How do I check if an array includes a value in JavaScript?

Modern browsers have Array#includes, which does exactly that and is widely supported by everyone except IE:

console.log(['joe', 'jane', 'mary'].includes('jane')); //true

Check if an array includes an array in javascript

.includes() method uses sameValueZero equality algorithm to determine whether an element is present in an array or not.

When the two values being compared are not numbers, sameValueZero algorithm uses SameValueNonNumber algorithm. This algorithm consists of 8 steps and the last step is relevant to your code , i.e. when comparison is made between two objects. This step is:


  1. Return true if x and y are the same Object value. Otherwise, return false.

So in case of objects, SameValueZero algorithm returns true only if the two objects are same.

In your code, since the array [1, 2] inside arr array is identically different to [1, 2] that you passed to .includes() method, .includes() method can't find the array inside arr and as a result returns false.

JavaScript: How can i check if an array contains elements in common with a second array?

A quick solution could be something like this:

const pcNumbers = [1,2,3,8,5];
const userNumbers = [1,2,7,8,9];
const newArr = [];

for (let i = 0; i < pcNumbers.length; i++) {
for (let j = 0; j < userNumbers.length; j++) {
if (pcNumbers[i] === userNumbers[j]) {
newArr.push(pcNumbers[i])
}
}
};

console.log(`These are in common: ${newArr.join(', ')}`)

This will result in:

These are in common: 1, 2

How to determine if Javascript array contains an object with an attribute that equals a given value?

2018 edit: This answer is from 2011, before browsers had widely supported array filtering methods and arrow functions. Have a look at CAFxX's answer.

There is no "magic" way to check for something in an array without a loop. Even if you use some function, the function itself will use a loop. What you can do is break out of the loop as soon as you find what you're looking for to minimize computational time.

var found = false;
for(var i = 0; i < vendors.length; i++) {
if (vendors[i].Name == 'Magenic') {
found = true;
break;
}
}

Check if an array contains values from object and return them?

This will return the object as you asked (In case if it existing)

let resultWithSomethingB = array.filter(element => element.something === 'b')[0];

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));


Related Topics



Leave a reply



Submit