ES6 version of this is (check out answer from Allison):
!str1.includes(str2)
The original accepted answer was:
You are looking for indexOf
var x = "home.subjects.subject.exams.exam.tests";
console.log(x.indexOf('subjects')); // Prints 5
console.log(x.indexOf('state')); // Prints -1
Answer from Ananth on Stack OverflowES6 version of this is (check out answer from Allison):
!str1.includes(str2)
The original accepted answer was:
You are looking for indexOf
var x = "home.subjects.subject.exams.exam.tests";
console.log(x.indexOf('subjects')); // Prints 5
console.log(x.indexOf('state')); // Prints -1
You could use includes and negate it.
!str1.includes(str2)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
Hi All,
Trying to get a little function going where when a client leaves a * or symbol in a certain table cell, the background color of that table cell will change.
This is all in a `if is_page())` and I'm also using ACF.
I receive this error in Chrome Console:
Uncaught TypeError: cells.includes is not a function
Any assistance would be great!
UPDATE: SOLUTION IS BELOW
Solution:
<script type="text/javascript">
function cellColorBlue() {
var tableCells = [...document.querySelectorAll("td")];
for (cell of tableCells) {
if(cell.innerText.includes("*")) {
cell.style.backgroundColor = "#63b2df";
}
}
}
function cellColorGreen() {
var tableCells = [...document.querySelectorAll("td")];
for (cell of tableCells) {
if(cell.innerText.includes("^")) {
cell.style.backgroundColor = "#9ce158";
}
}
}
cellColorBlue();
cellColorGreen();
</script>Just negate it.
filteredResult = filteredResult.filter(e => !e.selectedFields.includes("Red"))
You can negate the includes result or even cleaner just by using the indexOf property:
const arr = [1, 2, 3];
const notIncludes1 = (arr, val) => !(arr.includes(0));
const notIncludes2 = (arr, val) => arr.indexOf(val) === -1;
Because Array in js is a specific object, the [2,4] inside myArray is object and [2,4] that you switch to includes is anther object. If you want that includes return true you must do this:
var array = [2, 4]
const myArray = [array, "cat", "hamster", 9]
console.log(myArray.includes(array))
A string is a value type, so a comparison between two strings will compare the value of those strings. In your case the value cat.
However, an array is an object with reference comparison, not value comparison. So when comparing two arrays the reference will be compared. That is if you compare the same object to itself the result will be true. However, as is the case in your example, if you compare two different objects even with all the properties set to the same value, the result will be false.
let a = [1,2];
let b = 2;
let c = "string";
let d = [1,2];
a === a; //true reference comparison comparing an object to itself
b === 2; //true value comparison
c === "string"; //true again value comparison, even though it's two different objects
a === d; //false the values are the same but it's reference comparison
Array.includes iterates through the array and makes a comparison between the argument and the individual elements using the above comparison types depending on the types.
It's also important to note that includes uses strict comparison. That is if a comparison with === results in true then so would includes. It's not enough that == would result in true. "2" == 2is an example of a comparison that returns true where ["2"].includes(2) returns false
If you look at the documentation of includes(), most of the browsers don't support this property.
You can use widely supported indexOf() after converting the property to string using toString():
if ($(".right-tree").css("background-image").indexOf("stage1") > -1) {
// ^^^^^^^^^^^^^^^^^^^^^^
You can also use the polyfill from MDN.
if (!String.prototype.includes) {
String.prototype.includes = function() {
'use strict';
return String.prototype.indexOf.apply(this, arguments) !== -1;
};
}
IE11 does implement String.prototype.includes so why not using the official Polyfill?
Source: polyfill source
if (!String.prototype.includes) {
String.prototype.includes = function(search, start) {
if (typeof start !== 'number') {
start = 0;
}
if (start + search.length > this.length) {
return false;
} else {
return this.indexOf(search, start) !== -1;
}
};
}
I have a line of code :
If (apple.getValue(true).contains(pear)) { assertNotEquals(pear, apple.getValue(true))
Question: How do I do the same line of code but have "not contains" instead of "contains"?