Check If an Element Is Present in an Array

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 ids 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



Leave a reply



Submit