Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I create a MongoDB selector from some session variables. If the session value is null, the selector should not include the condition.
I have a selector
return Session.get('title') ? { title: Session.get('title') } : {};
which works as it should; however, I want to filter on more session values.
If I add another one, I get
var title = Session.get('title');
var age = Session.get('age');
if (title && age) {
return {
title: title,
age: age
};
} else if (title) {
return {
title: title
};
} else if (age) {
return {
age: age
}
} else {
return {}
}
and it will only get more complicated when I add another session value.
How can I obtain the same result in a smarter way?
You could use an array to hold all of your possible values, then you just add to this array when a new value is added.
var sessionVars = ['title', 'age'];
var result = {};
sessionVars.forEach(function(varName) {
var value = Session.get(varName);
if (value) {
result[varName] = value;
}
});
return result;
Related
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 5 months ago.
Improve this question
hello i'm work for question system i want to add many answers in a one question in a one value
html
<input id="an" placeholder="test" type="text"/><a id="wo" style="display:none;">done test</a><button id="bt">Submit</button>
javascript
const an = document.getElementById("an");
const bt = document.getElementById("bt");
bt.addEventListener("click", () => {
if (an.value.toLowerCase() === "test") { // I want create many value in here.
bt.style.display = "none";
an.style.display = "none";
document.getElementById("wo").style.display = "initial";
} else {
bt.innerText = "Wrong!"
bt.style.background = "red";
}
});
I hope this answer is what you are looking for:
You can make an array with your answers, and then check to see if the given answer is inside the array.
const array = ['test', 'test2', 'test3'];
if (array.includes(an.value.toLowerCase() ) )
...
Something else, you are missing a semicolon in your else { ... }.
bt.innerText = "Wrong!";
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 have an array within an angular scope that basically contains an object within each iteration. I have used the indexOf() function before to check if a value exists within an array - but how can I check if a value exists within the object word value?
I want to use a foreach loop and just check if a word exists within the rules array in the format shown below - how is the best way to achieve this?
rules = [];
rules[0] = { word:"house"}
rules[1] = { word:"shoes"}
rules[2] = { word:"tools"}
rules = []
rules[0] = { word:"house"}
rules[1] = { word:"shoes"}
rules[2] = { word:"tools"}
rules[3] = { sentence:"horse"}
rules.forEach(rule => {
if (rule.word) {
console.log('Exist. Value:', rule.word)
} else {
console.log('Doesn\'t Exist.')
}
})
Hope this helps!
for(var i =0;i < rules.length;i++){
if(rules[i].word === 'valueYouWantToCheck'){
// do whatever you want to do
}
}
Try this...:)
You can use this
var rules = [];
rules[0] = { word:"house"}
rules[1] = { word:"shoes"}
rules[2] = { word:"tools"}
rules.forEach(function(item){
console.log(item.word) // your word of choice
})
You can also use filter function. If your required word is matched it will return the object or else it will return an empty array
var getValue = rules.filter(function(item){
return item.word=='house';
})
console.log(getValue)
Beside you can also use .find method
rules.find(function(item){
return item.word=="house";
})
rules = [];
rules[0] = { word:"house"}
rules[1] = { word:"shoes"}
rules[2] = { word:"tools"}
for(var i=0; i<rules.length ; i++)
{
rules[i]["word"]=='valueforComparison'; // or rules[i].word=='valueforComparison';
}
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 want to make a sidebar that dynamically create content from an object result in javascript
Object { Chemistry: Array[3], Maths: Array[1], Physics: Array[2] }
And also there are some element in Array above
I know this isn't the best way to do it, but this is an idea on how to make it:
var obj = {'Chemistry':['Chemistry1','Chemistry2','Chemistry3'],'Maths':['Maths1'],'Physics':['Physics1','Physics2']};
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function ulCreator(obj){
var menu = document.createElement("UL");
for (var key in obj) {
var node = document.createElement("LI");
var textnode = document.createTextNode(isNumber(key) ? obj[key] : key);
node.appendChild(textnode);
if(Array.isArray(obj[key])){
node.appendChild(ulCreator(obj[key]));
}
menu.appendChild(node);
}
return menu;
}
document.getElementById("main").appendChild(ulCreator(obj));
<div id="main">
</div>
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 6 years ago.
Improve this question
We are migrating from Ruby to NodeJS and we essentially wanted a function like this in Node:
starting_after = nil
charges = []
while true
results = Stripe::Charge.all(limit: 100, starting_after: starting_after)
break if results.data.length == 0
charges = charges + results.data
starting_after = results.data.last.id
end
How should one go about implementing this in NodeJS?
var stripe = require("stripe")(
"sk_test_xxx"
);
function paginateCharges(last_id) {
// Define request parameters
var req_params = { limit: 3 };
if (last_id !== null) { req_params['starting_after'] = last_id; }
// Get events
stripe.charges.list(
req_params,
function(err, charges) {
// Do something with the returned values
for (i = 0; i < charges.data.length; i++){
console.log(charges.data[i].id);
}
// Check for more
if (charges.has_more) {
paginateCharges(charges["data"][charges["data"].length - 1].id);
}
}
)
}
paginateCharges(null);
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 the following Javascript code:
var Lab = {
minseat: function(){
var seat = 10;
return seat;
}
maxseat: function(){
var seat = 50;
return seat;
}
}
So, when I need to get the value, I simply call the following code:
console.log(" Min Seat: " + Lab.minseat());
console.log(" Max Seat: " + Lab.maxseat());
However, it seems it is not working. So, may I know if it is possible to have a Javascript variable to have two or more functions inside?
You forgot a comma after the first function:
var Lab = {
minseat: function(){
var seat = 10;
return seat;
},
maxseat: function(){
var seat = 50;
return seat;
}
}
How about a comma separating the keys in your object definition.
var Lab = {
minseat: function(){
var seat = 10;
return seat;
},
maxseat: function(){
var seat = 50;
return seat;
}
}
http://jsfiddle.net/V4Yje/