This question already has answers here:
How do I check if a variable is an array in JavaScript?
(24 answers)
Closed 5 years ago.
I'm working on a chatbot in Javascript, and I have a variable called "target", which is the variable that will contain what the user says in chat, and an array called "doingGood":
let doingGood = ["good", "great", "amazing", "awesome"];
What I want to do, is I want to make an if statement that will do something if the target variable is equal to a string that's in the doingGood array. For example, if target is equal to good, great, amazing, or awesome, do something. I've looked around on the internet, but I'm stumped and can't seem to find an answer. Help would be appreciated, thanks!
EDIT: The question is the same, but I tried the methods that they suggested there and they didn't seem to work. When I tried if (variable.target === doingGood), it gave me an error saying "variable is not defined."
you can use indexOf methd of array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
if(doingGood.indexOf(target) !== -1){
// do something
}
Related
This question already has answers here:
Array.includes() to find object in array [duplicate]
(8 answers)
Object comparison in JavaScript [duplicate]
(10 answers)
How to determine equality for two JavaScript objects?
(82 answers)
Closed 9 months ago.
I'm trying to use arr.filter() to find the missing element between two arrays, this has been answered plenty of times and i've seen threads like this one Finding missing element in two array for javascript that explain it actually very well. For some reason thought, i cant seem to get this to work.
This is my code
var x = [{Number:1},{Number:2},{Number:3}]
var y = [{Number:1},{Number:2}]
function getDifference(x,y){
x.filter(function(object,index,arr){
console.log(object,index,arr)
if(!y.includes(object)){
// print object
console.log(object) // <-- Prints all 3, should just print the missing one.
}
})
}
getDifference(x,y)
Basically, it just needs to print out the missing object. Which in this case, is {Number:3}
Instead, it prints every object.
I've tried with the code in the thread that i linked above, and still was having trouble. Not sure what i'm not understanding with it.
This question already has answers here:
Use JavaScript to find a specific link
(4 answers)
Closed 3 years ago.
Before I add a link to a page, I want to check if it already exists anywhere on the page first.
I thought I could do :-
if (document.innerHTML.indexOf(link) != -1) {
But I get "Cannot read property 'indexOf' of undefined"
I wonder if there is a way to check if a link already exists on the page. I would prefer Javascript code as opposed to JQuery please.
Your help, much appreciated.
document.body.textContent.search("link")
Use body.textContent instead of innerHTML
If you get -1 as result then it means it does not exists else it will probably give you the position of the match.
Refer : https://www.w3schools.com/jsref/jsref_search.asp
and https://www.w3schools.com/jsref/prop_node_textcontent.asp
This question already has answers here:
Concise way to compare against multiple values [duplicate]
(8 answers)
Closed 4 years ago.
I want to check if a variable (in this case sampleVariable) is equal to THIS or THAT:
if(sampleVariable == "THIS" || chosenVerb == "THAT")
The above code should work fine, but I was wondering if there was a way to simplify and compact it - for example, something like this (this probably isn't how you do it, just an example):
if(sampleVariable == "THIS" || "THAT")
I'm fairly certain the above doesn't work since it will check for the two statements being true separately.
I found this website, which seems to be what I'm looking for. They say that this code is the best way to go around it:
if (fruit.match(/^(banana|lemon|mango|pineapple)$/)) {
handleYellowFruit();
}
Is this still the way that this is supposed to be done (since the blog post I linked above was published over half a decade ago)? If so, what are these characters in the parentheses: / ^ $ ?
Thanks for the help!
Depending on browser support and ability to polyfill, I'd try array.includes:
if ((["THIS", "THAT"]).includes(sampleVariable)) {
This question already has answers here:
Check if element exists in jQuery [duplicate]
(8 answers)
Closed 6 years ago.
Let's say we have:
var $letsTestA = $( '.lets-test-a' ),
$letsTestB = $( '.lets-test-b' );
While the HTML is:
<div class="lets-test-a"></div>
(I know, I left out .lets-test-b)
Now, what happens if we try to call the variable $letsTestB?
I did do some testing, I'm not just blindly asking a question without doing some research first, but I'm a little confused with how this seems to work...
If I alert($letsTestA); or alert($letsTestB); I get the same outcome which is just [object Object] when testing from JSFiddle?
Here's my test fiddle: https://jsfiddle.net/m6j03423/
Furthermore to this, what I'm actually trying to do here is create a way to find out whether the content of a variable exists or not.
In PHP I would just write if (empty($myVar)) { // do action } but JS doesn't work the same way.
In JS I think I can write if ($letsTestA) { } and it should be able to print the result of the variable? But, I'm still getting [object Object].
What am I missing?
Can I even print the contents of a jQuery object?
How can I test whether the variable contains the true value of a jQuery object?
EDIT:
Admittedly, I did see a few different answers saying to check the length, but I didn't understand that enough to connect that to what I'm doing.
You can use the .length property of the object to ascertain whether elements exists or not
if ($letsTestA.length > 0) {
//element exists
}
var $letsTestA = $('.lets-test-a'),
$letsTestB = $('.lets-test-b');
console.log($letsTestA.length);
console.log($letsTestB.length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="lets-test-a"></div>
This question already has answers here:
How can I unset a JavaScript variable?
(12 answers)
Closed 9 years ago.
With an Array I know you can use the delete keyword to remove items out of the Array. However, delete will not delete a variable.
var b = "some stuff";
delete b;
console.log(b); // "some stuff"
What's the correct way to "free" the memory used by b ? Does doing just b = null; do the trick?
Possible duplicate of this question about unsetting variables in Javascript with some excellent answers. Short answer - null is probably fine.
In Javascript, you cannot really 'free' any memory yourself; all you can do is remove all references to the memory that an object uses, and the JS engine's garbage collector will recover it. Setting the variable / property to null would be a good starting point.
As regards the use of 'delete', I haven't found a better resource for understanding it than http://perfectionkills.com/understanding-delete/.