I have multiple inputs (type="number") and when someone put a value into it i am creating a object. I like to create a statement and check if a specific object value is greater then 0.
if(el){
el.addEventListener('input', function(evt) {
const input = evt.target
if (input.checked || input.value.length) {
steps[input.dataset.name] = {
value: input.value
}
} else {
delete steps[input.dataset.name]
}
})
}
So my object i looking like that
So in object steps i have multiple objects where each has a unique name and a value. I like to run a function but only when a specific object value is greater then 0, in example when sale_monthly_additional_sales value is > 0 do something. I have no idea how to even start with that.
you could add condition like that.
Create array and add key name of the element name .
Then match with Array#includes().Finally apply you condition as you wish
CODE
var arr =['sale_monthly_additional_sales']; //
if (el) {
el.addEventListener('input', function(evt) {
const input = evt.target
if (input.checked || input.value.length) {
var val =input.value; // initiate default value
if(arr.includes(input.dataset.name)){ //match keyname on your element name
val = parseFloat(input.value) > 0 ?'your code':val; //over write as you condition
}
steps[input.dataset.name] = {
value: input.value
}
} else {
delete steps[input.dataset.name]
}
})
}
Related
I'm running into a strange issue where I cannot access a field of an object I have defined unless I explicitly set that field to a number value.
I am trying to access the field with the following
if (userCache[mem.id][action.field]) {
this if block is not being entered. I am console.log()'ing the above value before this conditional, and it shows the value as 0, which is what it was previously set to using:
if (!isNaN(action.fieldvalue)) {
valToSet = Number(action.fieldvalue);
} else {
valToSet = action.fieldvalue;
}
userCache[mem.id][action.field] = valToSet;
Where action.fieldvalue is the value I want to set, but it's a string. If I instead set the value using = 0 , instead of = Number(), then I am able to access it correctly in the conditional.
This should help you understand the scenario:
$ node
> const a = '0';
> const b = Number('0');
> if (a) { console.log("true") } else { console.log("false") }
> if (b) { console.log("true") } else { console.log("false") }
To clarify, "0" string is not a falsy value since its not empty but 0 number is.
Here, there is a global variable. (An array type)
var obj = [];
Gets the value entered in input.
var d_idx = $('#d_idx').val();
var d_firstDate = $('#firstDate').val();
var d_secondDate = $('#secondDate').val();
var chozyintime = $('#ri_chozyinTime').val();
var zaezyintime = $('#ri_zaezyinTime').val();
"chozyintime" and "zaezyintime" are declared as arrays.
Because you can input multiple values.
var chozyinArray = [chozyintime];
var zaezyinArray = [zaezyintime];
At this time, I gave the condition. I will explain it later.
first,
if(obj.length <= 0)
{
firstAddData(d_idx, d_firstDate, d_secondDate, chozyinArray, zaezyinArray);
}
If the size of the initial obj is zero, push the input value to obj.
firstAaddData function is :
function firstAddData(d_idx, d_firstDate, d_secondDate, chozyinArray, zaezyinArray)
{
obj.push
(
{
"d_idx" : d_idx,
"ri_firstDate" : d_firstDate,
"ri_secondDate" : d_secondDate,
"ri_chozyinTime" : chozyinArray,
"ri_zaezyinTime" : zaezyinArray
}
);
}
Second obj.length> = 1,
At this time, the conditions described above are set.
This is, Of the values of the first pushed obj,
d_firstDate, and d_secondDate are compared with the newly inputted d_firstDate and d_secondDate.
else if(obj.length >= 1)
{
var filterObj = obj.filter(function(cur)
{
return cur.ri_firstDate == d_firstDate && cur.ri_secondDate == d_secondDate;
})
if(filterObj.length > 0)
{
filterObj.forEach(function(cur, idx)
{
if(chozyintime != "" && chozyintime != null)
{
cur.ri_chozyinTime.push(chozyintime);
}
})
}
else
{
firstAddData(d_idx, d_firstDate, d_secondDate, chozyinArray, zaezyinArray);
}
}
As a result, I got the following output.
{ri_zaezyinTime=[1231,1421,2561], ri_firstDate=2017-12-15, ri_chozyinTime=[5212, 2314], ri_secondDate=2017-12-26, d_idx=1}
However, exception handling is not implemented for duplicate values.
When you add a new input value (chozyintime), Compared to the n elements in the array, I want to make the alert window pop up when there are duplicate values.
How should I write code to implement what I want to implement?
I need your feedback. Help.
I have this object (that has a lot more properties than listed here, having different values):
var dbValuesObj = {
//1. group of properties that I don't want to touch(I don't want to check the values for these)
rebateNetSaleMinAmt1: 500,
rebateNetSaleMaxAmt1: 400,
rebateAmtWoProd1: 0,
rebateAmtAllProd1: 200,
rebateAmtOneProd1: 0,
//2. group of properties that I want to change (I know exactly what is the list of the properties that I want to check and change their values, if they are equal to 0)
rebateNetSaleMinAmt2: 100,
rebateNetSaleMaxAmt2: 0,
rebateAmtWoProd2: 300,
rebateAmtAllProd2: 0,
rebateAmtOneProd2: 700
}
I need to change the values for the object properties from point #2 that have value 0.
So in my case, I want to change rebateNetSaleMaxAmt2 and rebateAmtAllProd2 to a different value. But I don't want to check or change properties from #1
I have tried using
for(var property in dbValuesObj)
but I checks all the properties and I don't want to look/change/check the properties from #1
I need to change the values for the object properties from point #2
that have value 0. So in my case, I want to change
rebateNetSaleMaxAmt2 and rebateAmtAllProd2 to a different value.
You can filter them out first
var requiredKeys = Object.keys( dbValuesObj ).filter( k => k.slice(-1) == "2" && dbValuesObj[k] == 0 );
Now you can iterate this requiredKeys array
requiredKeys.forEach( function( key ){
console.log( key, dbValuesObj[key] )
})
For data you have provided, it will print
"rebateNetSaleMaxAmt2" 0
"rebateAmtAllProd2" 0
You need to check whether the property ends with "2" and whether the property's value is 0.
To get the last value from the property string you can do the following:
property[property.length-1];
Then to get the value of the property you can do this:
dbValuesObj[property];
Now that you know how to get required components you can use an if statement in you for loop to check whether or not the property meets your requirements (ends with "2" and has a value of 0) and then change the value to whatever you want it to be. (in the code snippet I am setting it to 1)
Check the code snippet for a working example:
var dbValuesObj = {
rebateNetSaleMinAmt1: 500,
rebateNetSaleMaxAmt1: 400,
rebateAmtWoProd1: 0,
rebateAmtAllProd1: 200,
rebateAmtOneProd1: 0,
rebateNetSaleMinAmt2: 100,
rebateNetSaleMaxAmt2: 0,
rebateAmtWoProd2: 300,
rebateAmtAllProd2: 0,
rebateAmtOneProd2: 700
}
for (var property in dbValuesObj) {
var value = dbValuesObj[property];
if (property[property.length - 1] === "2" && value === 0) {
dbValuesObj[property] = 1; // Change the value to a new value (ie 1)
}
}
console.log(dbValuesObj);
function updateGroup(obj, defaultValue, grpNum) {
for(prop in obj ) {
var index = parseInt(prop.match(/\d+$/)[0]);
if(index === grpNum && obj[prop] === 0) {
obj[prop] = defaultValue;
}
if(index > grpNum) { // if properties are in number order. else remove this.
return;
}}
return;
}
updateGroup(dbValuesObj, 100, 2);
You can try this by giving groupNumber and defaultValue you what to set for 0 in that group.
So this is how i resolved my issue
Thanks everyone for the answers
//Define the properties that we want to check for 0 value
var arrayOfPropsToCheck = ["rebateNetSaleMinAmt1", "rebateNetSaleMaxAmt1", "rebateAmtWoProd1", "rebateAmtAllProd1", "rebateAmtOneProd1",
"rebateNetSaleMinAmt2", "rebateNetSaleMaxAmt2", "rebateAmtWoProd2", "rebateAmtAllProd2", "rebateAmtOneProd2",
"rebateNetSaleMinAmt3", "rebateNetSaleMaxAmt3", "rebateAmtWoProd3", "rebateAmtAllProd3", "rebateAmtOneProd3",
"rebateNetSaleMinAmt4", "rebateNetSaleMaxAmt4", "rebateAmtWoProd4", "rebateAmtAllProd4", "rebateAmtOneProd4",
"rebateNetSaleMinAmt5", "rebateNetSaleMaxAmt5", "rebateAmtWoProd5", "rebateAmtAllProd5", "rebateAmtOneProd5",
"rebateNetSaleMinAmt6", "rebateNetSaleMaxAmt6", "rebateAmtWoProd6", "rebateAmtAllProd6", "rebateAmtOneProd6",
"rebateNetSaleMinAmt7", "rebateNetSaleMaxAmt7", "rebateAmtWoProd7", "rebateAmtAllProd7", "rebateAmtOneProd7"];
//Check the properties in the dbValuesObj object
for (var property in dbValuesObj) {
//Get the value for the property
var value = dbValuesObj[property];
//Define flag
var isInArray = false;
//Get the values from the arrayOfPropsToCheck
for(var k = 0; k < arrayOfPropsToCheck.length; k++){
//Compare if the name of the property is equal to the one found in the array and set flag to true
if(arrayOfPropsToCheck[k] == property){
isInArray = true;
}
}
//If propery is in arrayOfPropsToCheck and has value 0 change it to empty string
if(isInArray && value === "0"){
dbValuesObj[property] = "";
}
}
I'm trying to apply a constraint on combobox. It's half-working at the moment.
On the combobox, I have this listener:
[...]
listeners: {
'focus': function (combo, value) {
var orgComboVal = Ext.getCmp('Org1')
var getOrgValue = orgComboVal.getValue();
if (typeof getOrgValue !== undefined) {
reseaulist.clearFilter(true);
for (var q = 0, l = getOrgValue.length; q < l; q++) {
reseaulist.filter([
{property:'code_organisme', value: getOrgValue[q]}
]);
}
}
}
}
Ext.getCmp('Org1') defines another combobox.
When orgComboVal.getValue() is a single value, the filter is well applying.
but when it's an array of value, eg ["5", "9"], it's not working and the combobox supposed to be filtered shows no value (so I guess a filter is still applied but in an incorrect way).
I guess it's because the reseaulist.filter is called multiple time.
How can I achieve this ?
I saw the filterBy method but I don't know how to make it work.
Also, this post is interesting : How to filter a store with multiple values at once? but same, can't make it work since
getOrgValue.split(',')
is showing an error
(Object Array has no method split)
Any tips ? I'm using ExtJS 4.2.
EDIT
Thanks to #rixo, I've made it.
Also, I had to change some of the code he provided me, because the value of the Org1 combobox was always an array, even if empty, so the store filter was never cleared.
Here it is :
'focus': function (combo, value) {
var orgComboVal = Ext.getCmp('Org1')
var values = orgComboVal.getValue();
console.log(values)
if (values != null) {
reseaulist.clearFilter(false);
if (Ext.isArray(values)) {
if (0 < values.length) {
reseaulist.filterBy(function(record, id) {
return Ext.Array.contains(values, record.get('code_organisme'));
});
} else {
reseaulist.clearFilter(true);
}
}
}
}
Each filter is applied one after the other on the previously filtered data set, so your code implements a logical AND. That's why all values are filtered out...
Here's an example using filterBy to accept any value that is in your array:
function (combo, value) {
var orgComboVal = Ext.getCmp('Org1')
var values = orgComboVal.getValue();
if (values != null) {
store.clearFilter(false);
if (Ext.isArray(values)) {
store.filterBy(function(record, id) {
return Ext.Array.contains(values, record.get('code_organisme'));
});
} else {
record.get('code_organisme') === values;
}
} else {
store.clearFilter(true);
}
}
Or you could also use a regex with the filter method:
function (combo, value) {
var orgComboVal = Ext.getCmp('Org1')
var values = orgComboVal.getValue();
if (values != null) {
var filterValue = Ext.isArray(values)
? new RegExp('^(?:' + Ext.Array.map(values, function(value){return Ext.escapeRe(value)}).join('|') + ')$')
: values;
store.clearFilter(false);
store.filter('code_organisme', filterValue);
} else {
store.clearFilter(true);
}
}
Concerning your error, arrays indeed don't have a split method. Strings can be split into an array. Arrays, on their side, can be joined into a string...
Try This....
var getOrgValue = "5,9,4"; // array of value
reseaulist.filterBy(function(rec, id) {
return getOrgValue.indexOf(rec.get('code_organisme')) !== -1;
});
So I'm taking a form and using serializeArray() to get all the forms data. There are 10 text inputs. What I am trying to do is skip the forms that return a empty result or forms that have "" as value. Here is what I came up with and it returns the index of the forms with "" or empty results correctly.
$("#" + forms).on("submit", function(event) {
var allData = $(this).serializeArray();
event.preventDefault();
for (var key in allData) {
if (allData[key].value === "") {
allData.splice(key, 1);
}
}
});
But when I add allData.splice(key, 1); it doesn't remove all the values with "" as a result. I basically want to remove any input that isn't going to have a value.
Also the structure of the objec is as follows.
[0]
name: "emailone",
value "some#email.com"
[1]
name: "passwordone",
value: "123asd"
[2]
name: "emailtwo",
value "another#email.com"
[3]
name: "passwordtwo",
value: "asd123"
[4]
name: "emailthree",
value ""
[5]
name: "passwordthree",
value: ""
That happens because you are altering the array while traversing it...
When you key is 4 and the value is '' you remove that element (succesfully) but when you splice the array it becomes a smaller one.. so the element at position 5 is now at 4. Your key variable is increased to 5 but now there is no element 5 in your array..
You need to traverse it backwards
$("#" + forms).on("submit", function(event) {
var allData = $(this).serializeArray();
event.preventDefault();
for (var key = allData.length-1; key >= 0 ; key--) {
if (allData[key].value === "") {
allData.splice(key, 1);
}
}
});
By splicing an array as you iterate over it, you can accidentally skip values - in this case, by removing a value at index four, you decrease the index of the following value by one. The loop than increments to five, and the value that started at the fifth index is skipped.
A few other answers have posted reasonable ways to work around this. If you're working with a newer version of JavaScript or with a library like jQuery or underscore, you could alternatively do this:
allData = allData.filter(function(e) {
return e.value !== "";
});
With jQuery, this can be written as
allData = $.grep(allData, function(e) {
return e.value !== "";
});
and in underscore:
allData = _.filter(allData, function(e) {
return e.value !== "";
});
var arrLength = allData.length, i, cleanArray = [], serializedArr;
for(i = 0; i < arrLength; i += 1{
if(allData[i] != ""){
cleanArray.push(allData[i]);
}
}
serializedArr = cleanArray.serializeArray();
You simply use delete to achieve your need,
for (var key in allData) {
if (allData[key].value === "") {
delete allData[key];
}
}
additionally you don't need explicitly get the value by this toallData[key].value, toallData[key] will simply return the value associated with the particular key.
DEMO
Updated:
for (var i=0; i < allData.length ; I++) {
if (allData[i].value === "") {
allData.splice(i, 1);
i--;
}
}
You should decrement the value of the index by one, every time you are removing an object from the array.