i have a amount field some of which is empty, so ...
# suitescript
v
i have a amount field some of which is empty, so i keep it in the array only if it has amount. how can i have it in if statement?
r
You can filter out the empty amounts from the search/query itself. With an amount greater than 0. Otherwise in script logic it could be something as simple as if(amount && amount>0) You should check how a variable with values eifher null, undefined, empty string works while checking conditions like if(variableName). This is more towards how JS behaves rather than suitescript.
v
if(resultArray[i].Amount !== 0 || resultArray[i].Amount !== null) I tried like this, but still it keeps the empty value into array.
c
What rag is saying is you use the conditional logic to stop it being put into the array in the first place
v
yes thats what i am trying to do. i am trying the logic , but nothing workd
s
Not sure I follow, but are you saying you are starting with an array, resultArray, and you just want to remove entires with an empty amount from it? Or, are you building a new array based on the value of resultArray[i].Amount? You aren’t showing the entire code section, so it’s hard to know
But, the bigger problem is the if statement is always going to be true. In order to be false, Amount would have to be both 0 and Null at the same time, which is not possible. You probably want && not ||
v
This is my code if(amount !== 0 && amount !== null){ amount.push(amount[i]) } Still it puts the empty values into array but not the zero values
s
what are the “empty” values? are they undefined or empty strings? because if amount is undefined or an empty string, then the if statement is still incorrect. in fact, it sounds more like you want to exclude all falsy values, so just using if (amount) may be correct here (where 0, null, undefined, and
''
are all falsy)
actually, after you posted more code, i can see that it’;s just plain wrong
You are checking if amount is zero or null, but then taking element at index i from amount, and pushing it back into amount. This makes it look like amount is an array. If so, the if statement is completely wrong. There’s also no iteration of the array happening here.
v
if(amount[i] !== 0 && amount[i] !== null){ amount.push(amount[i]) }
this is the one still not working, it puts the empty values into array
s
i still don’t understand why you are pushing an element from amount that you want to filter out, back onto the same array it came form (amount). You can never get rid of unwanted elements this way. In fact, you are duplicating elements that are not 0 or null.
you’re assumption is mistaken. it’s not putting the 0 or null elements into the array, they are simply staying there. but you are adding copies of the non-0 and non-null elements
r
You are fetching from the amount array and pushing the amount array ?
s
Array.push adds something to an existing array, it doesn’t remove anything
r
At this point I will suggest do a JS or any OOPS programming course. It should not take more then a couple of days to clear the basics before you move towards suitescript.
s
Strongly second this. Arrays and boolean logic are very basic, fundamental concepts in programming. You need to know what you are trying to accomplish and how those building blocks achieve your objective. Otherwise you will face more frustration through trial and error, and not understanding what is wrong