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] = "";
}
}
Related
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]
}
})
}
I have a table, and I want based on a property, to color rows like this:
If account is the same, color them with gray, and if not, color them with blue. Here is my code:
func() {
for (let i = 0; i < this.List.length; i++) {
if (this.List[i].account == this.List[i + 1].account) {
this.List[i].color = "#f2f3f4"
} else {
if (this.List[i].account != this.List[i + 1].account && this.List[i].color != "#f2f3f4") {
this.List[i].color = "rgba(173, 216, 230, 0.35)"
}
}
}
}
but it's not working properly. How can I modify the code? Here is a working blitzstack
I also get this error: (I guess it's from List[i + 1])
ERROR Error: Cannot read property 'account' of undefined
Try this in your code
func() {
const d = this.List.map(e => e.account);
this.List.forEach(user => {
const length = this.List.filter(s => s.account === user.account).length;
user.color = length >= 2 ? "#f2f3f4" : "rgba(173, 216, 230, 0.35)";
})
}
Basic index mistake.
You are looping over the whole array (from 0 to this.List.length) and then try to access this.List[i + 1].
Loop from 0 to this.List.length - 1
for (let i = 0; i < this.List.length - 1; i++) {
This answer is only valid if the requirement is:
All rows which have either the previous or next row with the same account must be colored in grey. Otherwise, they are colored in blue
A single row is in blue (cannot have same account with some other row)
An account which appears multiple times but non in consecutive rows are colored in blue
rows are assumed unsorted (so sorting rows again will make rows color change)
Additionally, it is good to know that in JavaScript forEach is supposed to follow array order.
forEach() calls a provided callback function once for each element in an array in ascending order. It is not invoked for index properties that have been deleted or are uninitialized (i.e. on sparse arrays).
The idea is:
Loop through all elements but order should not matter!
If the previous or next row has the same account, then apply the rules above
func() {
// Empty list
if(this.List.length === 0){
return
}
// A single entry cannot have "same account"
if(this.List.length === 1){
this.List[0].color = "rgba(173, 216, 230, 0.35)"
return
}
this.List.forEach((entry, i) => {
// beware of edge case (first and last rows)
const sameAccountPrev = i > 0
? entry.account === this.List[i-1].account
: false
const sameAccountNext = i < this.List.length -1
? entry.account === this.List[i+1].account
: false
entry.color = sameAccountPrev || sameAccountNext
? "#f2f3f4"
: "rgba(173, 216, 230, 0.35)"
});
}
Another good way to solve this would be to hash the usernames into a color. That way every two rows with the same user would have the same color.
Of course you would have less control over which color would be used.
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 a localStorage file made up of numeric keys. To write a new key, value pair to the file, I want to make sure the key does not already exist. If it does exist, I add one to the key and test again if it's present. It's a small set, so I thought this could be handled with a straightforward iteration of all keys and a check for match, e.g.:
var CheckItAintThere = function() {
for ( var i = 0, len = window.localStorage.length; i < len; ++i )
{
var Foundit = false;
console.log('checking for duplicate to see if '+window.localStorage.key(i)+' is equal to '+SaveNumber);
if (window.localStorage.getItem( window.localStorage.key(i) ) == SaveNumber.toString())
{
console.log('FOUNDIT TRUE');
Foundit = true;
}
}
while (Foundit == true) {
SaveNumber = SaveNumber + 1;
if (localStorage.getItem( localStorage.key( i ) ) !== SaveNumber.toString())
{
console.log('SaveNumber = '+SaveNumber+' LocalStoragekey = '+localStorage.key(i));
Foundit = false;
}
}
}
But Foundit never tests true, even when the console.log reports:
> checking for duplicate to see if 0 is equal to 3
> checking for duplicate to see if 1 is equal to 3
> checking for duplicate to see if 3 is equal to 3
I tried adding .toString() to the key, but as I understand it keys are stored as strings anyway, and this had no impact.
I have a feeling I'm going to have a palmprint on my forehead, but what am I doing wrong?
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.