I am new to JS. I want to include all the unavailable objects of Value while filtering. In the code below, I am applying the condition Value >= 4 && Value < 6. I want (Value >= 4 && Value < 6) || unavailable values of Value`
var Annual = [{"Date":1998,"Value":6.5,"GDPAnn":9062800},{"Date":1999,"GDPAnn":9631200},{"Date":2000,"Value":4.1,"GDPAnn":10251000},{"Date":2001,"GDPAnn":10581900}]
result = Annual.filter(function(v) { return v.Value >= 4 && v.Value < 6; })
console.log(result);
Add a !v.Value condition to your boolean expression
var Annual = [{"Date":1998,"Value":6.5,"GDPAnn":9062800},{"Date":1999,"GDPAnn":9631200},{"Date":2000,"Value":4.1,"GDPAnn":10251000},{"Date":2001,"GDPAnn":10581900}]
result = Annual.filter(function(v) { return (v.Value >= 4 && v.Value < 6) || !v.Value; })
console.log(result);
Edit:
Like said in a comment below, in a case that you would not to consider that any falsy value is invalid (like zero, empty string, etc), you might prefer using the Object.prototype.hasOwnProperty method.
const Annual = [{"Date":1998,"Value":6.5,"GDPAnn":9062800},{"Date":1999,"GDPAnn":9631200},{"Date":2000,"Value":4.1,"GDPAnn":10251000},{"Date":2001,"GDPAnn":10581900}]
const result = Annual.filter(function(v) {
return (v.Value >= 4 && v.Value < 6) || !v.hasOwnProperty("Value");
})
console.log(result);
typeof check if value undefined make the job too.
const Annual = [{"Date":1998,"Value":6.5,"GDPAnn":9062800},{"Date":1999,"GDPAnn":9631200},{"Date":2000,"Value":4.1,"GDPAnn":10251000},{"Date":2001,"GDPAnn":10581900}]
result = Annual.filter((v) => v.Value >= 4 && v.Value < 6 || typeof v.Value === "undefined");
console.log('res',result);
use hasOwnProperty method to check if the object has a property called Value and negate it.
var Annual = [{"Date":1998,"Value":6.5,"GDPAnn":9062800},{"Date":1999,"GDPAnn":9631200},{"Date":2000,"Value":4.1,"GDPAnn":10251000},{"Date":2001,"GDPAnn":10581900}]
result = Annual.filter(function(v) { return (v.Value >= 4 && v.Value < 6) || !v.hasOwnProperty('Value') })
console.log(result);
You can achieve that in different ways.
If falsy value which is 0 is not a valid one then you can use !v.value along with the v.Value >= 4 && v.Value < 6
const Annual = [{"Date":1998,"Value":6.5,"GDPAnn":9062800},{"Date":1999,"GDPAnn":9631200},{"Date":2000,"Value":4.1,"GDPAnn":10251000},{"Date":2001,"GDPAnn":10581900}]
const result = Annual.filter(v => (v.Value >= 4 && v.Value < 6) || !v.Value)
console.log(result);
You can use Object.hasOwnProperty() method to check if property exist or not.
const Annual = [{"Date":1998,"Value":6.5,"GDPAnn":9062800},{"Date":1999,"GDPAnn":9631200},{"Date":2000,"Value":4.1,"GDPAnn":10251000},{"Date":2001,"GDPAnn":10581900}]
const result = Annual.filter(v => (v.Value >= 4 && v.Value < 6) || !v.hasOwnProperty('Value'))
console.log(result);
I would like to check any date conflicts occurring in my date array. I have an array like this.
[['2020-07-03T18:30:00.125000Z','2020-07-04T01:30:00Z'],['2020-07-03T18:30:00.125000Z','2020-07-04T00:30:00Z'],['2020-07-03T18:30:00.125000Z','2020-07-04T00:30:00Z']]
The first date in individual array is start date and end date respectively. What I want to check here is, the first array date is conflicting with the following dates in the array. So that I can assign a person based on the date. One person can assign to a single date time. So anybody knows the perfect ES6 way to solve this?
I have created a function to solve this question. dateTimes is the array which contains the dates.
checkDateTimeOverlap = (dateTimes)=>{
let isOverlap = false;
dateTimes.forEach((time,i) => {
let st1 = time[0];
let et1 = time[1];
dateTimes.forEach((time2,j) => {
if(i != j){
let st2 = time2[0];
let et2 = time2[1];
if (st1 >= st2 && st1 <= et2 || et1 >= st2 && et1 <= et2 || st2 >= st1 && st2 <= et1 || et2 >= st1 && et2 <= et1) {
isOverlap = true;
}else{
isOverlap = false;
}
}
})
});
return isOverlap;
}
I'm new to JavaScript and have just been toying with this little IF-ELSE exercise. Essentially, slots 1 thru 4 are static to experiment with the || operator. The const 'testSlot' is one that I've altered as time passes to try and execute the "else if" segment of my code; e.g, if it's 9:10 PM I've
just been manually changing the getHours to 21 and the getMinutes to 10 and then run the code.
With that said, I can't get either of the first two console.logs to run, it always just runs the "else", which is three.
My question is mostly if my usage the of the date object was wrong or if the syntax in my If-Else statement was wrong. A pointer in the right direction would be very much appreciated.
Here is my code:
const now = new Date();
const slot1 = now.getHours === 12 && getHours.getMinutes === 1;
const slot2 = now.getHours === 12 && getHours.getMinutes === 2;
const slot3 = now.getHours === 12 && getHours.getMinutes === 3;
const slot4 = now.getHours === 12 && getHours.getMinutes === 4;
const testSlot = now.getHours === 20 && getHours.getMinutes === 34;
if (slot1 || slot2 || slot3 || slot4) {
console.log('one');
} else if (testSlot) {
console.log('two');
} else {
console.log('three');
};
.getHours() and .getMinutes() are both functions and require parenthesis after. Also, getHours.getMinuets() wouldn't do anything. You have to do now.getMinutes(). I updated your snippet for you. It will still console.log three but that's only because all the if statements are false. Wait till its 12:01 and it should say one.
const now = new Date();
const slot1 = now.getHours() === 12 && now.getMinutes() === 1;
const slot2 = now.getHours() === 12 && now.getMinutes() === 2;
const slot3 = now.getHours() === 12 && now.getMinutes() === 3;
const slot4 = now.getHours() === 12 && now.getMinutes() === 4;
console.log(now.getHours());
console.log(now.getMinutes());
const testSlot = now.getHours() === 20 && now.getMinutes() === 34;
if (slot1 || slot2 || slot3 || slot4) {
console.log('one');
} else if (testSlot) {
console.log('two');
} else {
console.log('three');
};
I am trying to organize a observablearray that has inside 2 boolean values and a price. I need via knockout and 2 checkboxes, filter the elements by these two values. Also sort by price ( ascending and descending) the displayed values . I don't put any code because I'm new in knockout and I can't see the way to make these actions.
Appreciate someone who instructed me.
Simple answer, I tried with this, but making some changes on my personal viewModel to supply my needs. So, I make something like this:
self.elementsToShow = ko.pureComputed(function () {
// Represents a filtered and ordered list of elements
var recomend = self.showRecommended(); //chekbox 1
var special = self.showSpecial(); // checkbox2
var sorting = self.currentSortDirection(); //sort direction: price or rating //ascending or descending, represented by an observableArray with that conditions and the //selectedSortDirection
if (!recomend && !special) return self.myOservableArray().sort(function (a, b) {
//in case that no one of the checkboxes where selected but the sort direction was't by default
if (sorting.price != null) {
var fp = sorting.price ? -1 : 1;
ap = parseInt(a.price);
bp = parseInt(b.price);
return ap == bp ? 0 : (fp * (ap < bp ? -1 : 1));
}
else if (sorting.rate != null) {
var f = sorting.rate ? -1 : 1;
ar = parseFloat(a.rating);
br = parseFloat(b.rating);
return ar == br ? 0 : (f * (ar < br ? -1 : 1));
}
});
return ko.utils.arrayFilter(self.myOservableArray(), function (element) {
return (element.recommended != "0" && recomend) || (element.offer != "" && special); //some other conditions for the relection of the checkboxes in the observableArray
}).sort(function (a, b) {
if (sorting.price != null) {
var fs = sorting.price ? -1 : 1;
ap = a.price;
bp = b.price;
return ap == bp ? 0 : (fs * (ap < bp ? -1 : 1));
}
if (sorting.rate != null) {
var fu = sorting.rate ? -1 : 1;
ar = a.rating;
br = b.rating;
return ar == br ? 0 : (fu * (ar < br ? -1 : 1));
}
});
}, self);
function updategeneral() {
//tmp = "fine_" + tmp + "_";
var actual = doc.findItem("1speed").value;
var posted = doc.findItem("2speed").value;
amt = "";
if (1speed != "" && 2speed != "") {
var a = 1 * 1speed;
var b = 1 * 2speed;
if (a - b <= 9) {
alert(amt);
amt = doc.getDefault("general_spb_1_to_15");
} else if (a - b <= 15) {
amt = doc.getDefault("general_spb_16_to_25");
} else if (a - b <= 25) {
amt = doc.getDefault("general_spb_15_to_19");
} else if (a - b <= 29) {
amt = doc.getDefault("general_spb_26+");
}
doc.findItem("mcare_amount").value = amt;
alert(doc.findItem("mcare_amount").value = amt);
}
}
Default values are:
general_spb_1_to_15=30.00 || general_spb_16_to_25=40.00 || general_spb_26+=50.00
My problem is when amt is empty or 0 it is always going to general_spb_1_to_15=30.00. I am not sure how to fix this- can someone please help? The values that I am using are 1speed = 20 and 2speed = 25 which is negative or empty.
Assuming your browser engine is interpreting 1speed and 2speed as variables (some will, some won't -- variable names aren't supposed to start with numbers so it would probably be wise to replace these with speed1 and speed2)...
But assuming that, then the case that you describe is seeing a value of a - b = -5 when processing your if statements, which means that it is being caught by
if (a - b <= 9) {
alert(amt);
amt = doc.getDefault("general_spb_1_to_15");
}
To change the result you are getting, you should add another option to the if statement structure. Perhaps:
if (b > a)
{
//do something for this special case
} else if (a - b <= 9) {
alert(amt);
amt = doc.getDefault("general_spb_1_to_15");
} else if ...
You may also want to specifically handle the case where one or both of speed1/speed2 are empty as an else on the outer if block.