Check if an element is present in an array
ECMAScript 2016 incorporates an includes()
method for arrays that specifically solves the problem, and so is now the preferred method.
[1, 2, 3].includes(2); // true
[1, 2, 3].includes(4); // false
[1, 2, 3].includes(1, 2); // false (second parameter is the index position in this array at which to begin searching)
As of JULY 2018, this has been implemented in almost all major browsers, if you need to support an older browser a polyfill is available.
Edit: Note that this returns false if the item in the array is an object. This is because similar objects are two different objects 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
Function to verify if an element present in an array
int flag = checkElement(a1, element);
int checkElement(Array *a, int elem){
for (int i = 0; i < a->size; i++){
if (a->array[i] == elem){
return 1;
}
}
return 0;
}
Check if one element exists in an array of objects
I think this may help
let resultArray=memberships.filter(function(item) {
return item["type"] === 'member';
});
the result array holds the data of the objects that has type member
Check if object value exists within a Javascript array of objects and if not add a new object to array
I've assumed that id
s are meant to be unique here. some
is a great function for checking the existence of things in arrays:
const arr = [{ id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' }];
function add(arr, name) { const { length } = arr; const id = length + 1; const found = arr.some(el => el.username === name); if (!found) arr.push({ id, username: name }); return arr;}
console.log(add(arr, 'ted'));
How do I check whether an array contains a string in TypeScript?
The same as in JavaScript, using Array.prototype.indexOf():
console.log(channelArray.indexOf('three') > -1);
Or using ECMAScript 2016 Array.prototype.includes():
console.log(channelArray.includes('three'));
Note that you could also use methods like showed by @Nitzan to find a string. However you wouldn't usually do that for a string array, but rather for an array of objects. There those methods were more sensible. For example
const arr = [{foo: 'bar'}, {foo: 'bar'}, {foo: 'baz'}];
console.log(arr.find(e => e.foo === 'bar')); // {foo: 'bar'} (first match)
console.log(arr.some(e => e.foo === 'bar')); // true
console.log(arr.filter(e => e.foo === 'bar')); // [{foo: 'bar'}, {foo: 'bar'}]
Reference
Array.find()
Array.some()
Array.filter()
how to check if an element is present in an array or not in javascript without using for loop or any method of array
I suppose one option would be to convert the array to a Set
and check Set.has
:
let numList= [1,2,3,6,9,2,-8,20];const set = new Set(numList);console.log(set.has(9));console.log(set.has(30));
Check if item exists in array React
it should be:
handleCheck(val) {
return this.state.data.some(item => val.name === item.name);
}
because val
here is an Object not a String.
Check this out:
https://www.webpackbin.com/bins/-Kpo0Rk6Z8ysenWttr7q
Related Topics
How Many Bytes in a JavaScript String
Jquery to Loop Through Elements with the Same Class
Redirect a State to Default Substate with Ui-Router in Angularjs
How to Use Underscore.Js as a Template Engine
Removing an Anonymous Event Listener
How to Split a String into Segments of N Characters
How to Find the Height of Text on an HTML Canvas
How to Add a Tooltip to an Svg Graphic
Is There a JSON Equivalent of Xquery/Xpath
How to Set Multiple CSS Styles in JavaScript
How to Parse a Time into a Date Object from User Input in JavaScript
How to Convert an Integer to Binary in JavaScript