Comparing string to a list in javascript [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a list of car models that are in the list as Chevrolet,Ford,BMW, so I enacted this code on them.
cars.getMakes() returns a list with the names mentioned above and they are formatted as such.
cars.getAllModels() returns a similar list with just the car model names.
make is a single string that is either a model name or make name. Depends on users input.
I want to test to see if what the user put in actually exists in my predetermined lists.
if it does; true. If not; False.
function makeCheck(make) {
var models = cars.getAllModels();
var makes = cars.getMakes();
if (make == makes[make]) {
return true;
} else {
return false;
}
}

If you want to check that the variable make passed to the function is in the list of valid makes:
function makeCheck(make) {
var makes = cars.getMakes();
return (makes.indexOf(make) != -1);
}

Related

How to get a child key value in Firebase Realtime Database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm new with Firebase and I'm currently using JavaScript SDK to integrate my web application with Firebase.
My question is: How to get value within the red box that I drew on the image?
I am using
snapshot.key
to access the key valuebut it returns the generated key (blue box) to me which is not the one I wanted.
Based on your comments, it is not 100% clear what you are looking for. If you know that you have a sub-node with the key speechtext under the recording parent node with key LRC2o.... you don't need to query for getting the key of this sub-node (since you know it).
If, on the other hand, you want to iterate on all the keys of the sub-nodes of the recording node, do as follows (based on your code):
var dbRecording = firebase.database().ref("recordings/");
dbRecording.once("value", function(snapshot2) {
if (snapshot2.exists()) {
snapshot2.forEach(function(value) {
var childObject = value.val();
Object.keys(childObject).forEach(e => console.log(`key = ${e}`));
});
}
});
If you want the keys and the values, do as follows:
var dbRecording = firebase.database().ref("recordings/");
dbRecording.once("value", function(snapshot2) {
if (snapshot2.exists()) {
snapshot2.forEach(function(value) {
var childObject = value.val();
Object.keys(childObject).forEach(e => console.log(`key = ${e} value = ${childObject[e]}`));
});
}
});

Returning query fragments [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have an url that looks like this:
https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en
Is it possible to return every dk parameter separately? (no matter if there will be 1 or 5 dk parameters) so i would get separately sports, groupcompanyinsider, local.
If its not possible maybe there is a way to return all of them in one string like dk=sports&dk=groupcompanyinsiderlocal&dk=local ?
You can use the built-in javascript class URLSearchParams for this.
You can then transform this into the string you want with string concatenation and a foreach.
const url = "https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en";
var params = new URLSearchParams(url);
var result = "";
// concatenate individual values of the 'dk' query parameter
params.getAll('dk').forEach(function (item) {
result += '&dk=' + item;
});
result = result.substr(1); // remove starting '&' from the result;
console.log(result);
The result should contain your desired string.

Javascript form validation pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to validate that two characters and a number were correctly input.
var studentValid = /^[MTWTF][AL][1-9]$/i;
if (studentValid.test(studentTemp.value))
{
alert("true");
}
else
{
alert("false");
}
Yet everything I enter turns out false?
The problem is with your regexp (/^[MTWTF][AL][1-9]$/i). What this tells you is that you first want one of the characters M,T,W,T or F, and after that either A or L and finaly a number (and nothing before or after this).
So for example
ML4, WA5, FL9
will give you true
while
AM9, ML0, MMA5, MA99
will give you false.
Is this the pattern you are trying to match? There is nothing else wrong with your code and a valid value will give you true, for example:
var studentValid = /^[MTWTF][AL][1-9]$/i;
var value = 'MA9';
if (studentValid.test(value))
{
alert("true");
}
else
{
alert("false");
}
When working with regexp, it can be very usefull to use a tool to help you build it, check out https://regex101.com/r/A5FOIh/3 where you can try your different studentTemp.value to see if they match.

How to find that particular array Item is present or not Using JQuery + Backbone.js [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Each ArrayItem contains no. of Property like Id, Name, Description etc
But we want to Fetch ArrayItem with Help of Name Property .
So Please give me code suggestion in Jquery or backbonejs without using for loop.
If you are using BackboneJS, you already have UnderscoreJS installed. Underscore has several methods for searching through a collection. For instance, using _.findWhere(...):
var myArray = [ ... ];
var helpItem = _.findWhere(myArray, { name: 'help' });
This would return the first entry in the array that has a name property equal to 'help'. _.findWhere(...) can match multiple properties too. If you want to find an item with something other than direct equality of properties, you can use _.find(...):
var overTwentyOne = _.find(myArray, function(entry) {
return entry.age > 21;
});
This would return the first entry in the array whose age property was greater than 21.
Note also that most of the list-centric methods in Underscore are automatically mixed into all Backbone.Collection instances. So if you were searching through a Collection, the above findWhere(...) example could be more simply written as:
var helpItem = collection.findWhere({ name: 'help'});
This would return the first Backbone.Model instance from collection that had a property name equal to help.

jQuery filter options with value greater (less) than variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to disable values of options in select that are less than some number, which I do not know beforehand (it is set in another form element). I need something like code below, but with variable value of "variableInteger":
select.children().filter(function() {
return $(this).attr("value") > variableInteger;
}).each(function () {$(this).attr('disabled', 'disabled')});
Is there some clever way to do it?
PS. variableInteger value is from another form element, which name is also known only at runtime.
No need for the .each, also make use of prop and this.value (no need for $(this).attr("value");)
select.children("option").filter(function() {
return this.value > variableInteger;
}).prop("disabled", true);

Categories

Resources