searching values in array javascript [duplicate] - javascript

This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 9 years ago.
Is there a built-in function in javascript to do this or this is only the option to go? Please look at the code below:
var arr=[1,3,4,'+','-', or whatever]
function value_check(user_click){
var operators=['+','-','/','*','.']
for (var i=0;i<operators.length;i++){
if (arr[arr.length-1]==operators[i]){var value1='operator found';}
if (user_click==operators[i]){
var value2= value1;alert("consecutive operators"); break;
}
}
}
I think this code achieves what I intend to do but is there a simple and shorter way of doing this. In words, I want to achieve something like this:
if (arr[arr.length-1] && user_click BOTH ARE IN operators array)
alert("consecutive operators)

Yes, there are some options:
JavaScript indexOf()
jQuery.inArray()

arrayName.indexOf() is what you are looking for.

Related

Calling a nested function with a string (window[]) in JS [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 7 months ago.
How to call a nested function with only a string? eg:
function oot(wha) {
function inn(wha)
{
First.innerHTML+="Inner ["+wha+"]";
}
oot.inn = inn;
Second.innerHTML+="Outer ["+wha+"]";
}
oot("1");
oot.inn("2"); //works okay
window["oot"]("3"); //works okay
window["oot.inn"]("4"); //<The problem, doesn't work.
window["oot"]["inn"]("4"); //Works, thanks.
Edited to make the code more readable, and show a solution.
IF there is no way to make this work with a single string i can probably make do, but i will leave the question unanswered for a few hours to see if there is another solution.
You can reference nested Objects like this:
window["oot"]["inn"]("4");
or
window.oot.inn("4")

How are +variable+ and ${variable} different in javascript? [duplicate]

This question already has answers here:
ES6 template literals vs. concatenated strings
(4 answers)
Closed 1 year ago.
I'm start coding.
While I was watching a lecture, I saw code like this.
var coworkers = ['go', 'hello', 'hi', 'doit'];
<script type="text/javascript">
var i = 0;
while(i < coworkers.length){
document.write('<li>'+coworkers[i]+'</li>');
i = i + 1;
}
But But when I searched, Said to use ${variable} in JavaScript. and It didn't work.
How are +variable+ and ${variable} different in javascript? Thanks :)
Those placeholders, like ${variable}, can only be used in template literals, which are always enclosed in backticks(the key right below you escape key).
If it still doesn’t work, maybe you’re using an older browser?

why do you have to add the [I]? [duplicate]

This question already has answers here:
How to get value at a specific index of array In JavaScript?
(8 answers)
Closed 2 years ago.
so I am currently learning javascript on Codecademy. And a weird kind of thing got introduced that I don't quite get.
if you look at the code. you see at the end in console that after logging animals it is a [i] like why is that there? I get that is has something to do with the for loop. But I don't quite understand like why or what that it does. I don't know if question is clear enough but if you just try to explain why it is there and what it does there. that would be greatly appreciated:)
const animals = ['Grizzly Bear', 'Sloth', 'Sea Lion'];
for (let i = 0; i < animals.length; i++){
console.log(animals[i]);
}
the bracket notation is used to specify the index of the array you are looping through. You can always read more about javascript arrays on w3schools

Add double quotes to arraylist of string in javascript [duplicate]

This question already has answers here:
How to split comma separated string using JavaScript? [duplicate]
(4 answers)
Closed 3 years ago.
I am new to stack overflow learning javascript and programming.
I have a problem that i am stuck while learning and thinking any help on this question will be
useful for me thanks and the question is:
Example i have a variable a in the code below and i want to convert it to an array in javascript
var a = ["baby,cat,dog"]
i wanted it to be
a = ["baby","cat","dog"].
Use String.prototype.split() as below
var a = ["baby,cat,dog"];
a = a[0].split(',');
console.log(a);
You could take the array and map the splitted values and get a flat array back.
var array = ["baby,cat,dog"];
result = array.flatMap(s => s.split(','));
console.log(result);

How to put a or condition to .where function in underscore.js [duplicate]

This question already has an answer here:
How to use _.where method from underscore.js library for more elaborated searchs
(1 answer)
Closed 7 years ago.
I want to check or condition if the author is shakespeare it should return the value or if the year is 1611 also it should return the value.. how is the syntax in underscore
var a=_.where(listOfPlays, {author: "Shakespeare", year: 1611});
where uses AND logic.
For a different kind of condition consider using filter, giving it a relevant predicate, eg.
var result = _.filter(listOfPlays, function(play){
return play.author=="Shakespeare" || play.year==1611;
});

Categories

Resources