The JavaScript native .some() method does exactly what you're looking for:
function isBiggerThan10(element, index, array) {
return element > 10;
}
[2, 5, 8, 1, 4].some(isBiggerThan10); // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true
Answer from Alex Antonov on Stack OverflowThe JavaScript native .some() method does exactly what you're looking for:
function isBiggerThan10(element, index, array) {
return element > 10;
}
[2, 5, 8, 1, 4].some(isBiggerThan10); // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true
JavaScript has the Array.prototype.some() method:
[1, 2, 3].some((num) => num % 2 === 0);
returns true because there's (at least) one even number in the array.
In general, the Array class in JavaScript's standard library is quite poor compared to Ruby's Enumerable. There's no isEmpty method and .some() requires that you pass in a function or you'll get an undefined is not a function error. You can define your own .isEmpty() as well as a .any() that is closer to Ruby's like this:
Array.prototype.isEmpty = function() {
return this.length === 0;
}
Array.prototype.any = function(func) {
return this.some(func || function(x) { return x });
}
Libraries like underscore.js and lodash provide helper methods like these, if you're used to Ruby's collection methods, it might make sense to include them in your project.
(If you know C# LINQ , it's like Any vs All)
somewill return true if any predicate istrueeverywill return true if all predicate istrue
Where predicate means function that returns bool ( true/false) for each element
every returns on first false.
some returns on first true
some is analogue to logical or
every is analogue to logical and
logically every implies some, but not in reverse
try this:
var identity = function(x){return x}
console.log([true, true].some(identity))//true
console.log([true, true].every(identity))//true
console.log([true, false].some(identity))//true
console.log([true, false].every(identity))//false
console.log([false, false].some(identity))//false
console.log([false, false].every(identity))//false
console.log([undefined, true].some(identity))//true
console.log([undefined, true].every(identity))//false
console.log([undefined, false].some(identity))//false
console.log([undefined, false].every(identity))//false
console.log([undefined, undefined].some(identity))//false
console.log([undefined, undefined].every(identity))//false
Official document says
Using array notation has has some confusing differences from normal JSX:
Children in an array must be separated by commas.
Children in an array must have a key to prevent React’s key warning.
Strings must be wrapped in quotes.
So to make it simple, React provides Fragment component that can be used in place of arrays.
Consider how we can wrap multiple children using array
render() {
return [
"Some text.",
<h2 key="heading-1">A heading</h2>,
"More text.",
<h2 key="heading-2">Another heading</h2>,
"Even more text."
];
}
And how it can be achieved using Fragments.
render() {
return (
<Fragment>
Some text.
<h2>A heading</h2>
More text.
<h2>Another heading</h2>
Even more text.
</Fragment>
);
}
Taken directly from official document.
Fragments can be written as below aswell.
render() {
return (
<>
Some text.
<h2>A heading</h2>
More text.
<h2>Another heading</h2>
Even more text.
</>
);
}
Fragments and arrays are intended to address different use cases and behave differently in one important way.
Fragments will not warn if you omit a key property, while arrays will.
If your component returns several static children, return a fragment.
<Fragment>
<li>One advantage of our product is lorem</li>
<li>Another is ipsum!</li>
</Fragment>
If your component returns dynamically generated children, return an array.
items.map(item => <li key={item}>{item}</li>);
I am paraphrasing the maintainers' responses to an issue I opened in the React repo about a similar question. I highly recommend reading it for more detail: https://github.com/facebook/react/issues/12776
There is no contains method but you can use some()
Here is an example:
const exists = this.state.credentialsList.some(v => (v.user === credential.user && v.password === credential.password));
The code above checks if any of the objects inside the credentialsList array has the same user and password properties values as the credential object. You can modify the if condition if you don't want to check for both these properties.
I think the method you are looking for is includes
This will compare the entire object with the one given in params. For example:
const credentials = [{ username: 'foo', password: 'bar' }];
credentials.includes({ username: 'foo, password: 'bar' }); // true
credentials.includes({ username: 'foo, password: 'foobar' }); // false
If you want to compare specific values then the some method is more appropriate
credentials.some(cred => cred.username === this.state.username); // true