I can't help but wonder if there is better way to write this
{numberOfStudents && numberOfStudents > 1 && (
<div>do some jsx</div>
}
Is there a way to make this more succinct?
You can write a function to return the html and then include the function inside the render function.
All three of these return false:
console.log(null > 1)
console.log(undefined > 1)
console.log(0 > 1)
so you do not need the first condition so long as numberOfStudents is always a number | undefined | null.
{numberOfStudents > 1 && <div>...</div>}
Alternatively, if you check an array instead of a number value:
const students = [];
{students?.length > 1 && <div>...</div>}
Related
I want to sort Letters first and followed by numbers like below:
[Austria , France , Germany , 101110 , 124563]
This is what i have tried:
obj.sort((a,b) => a.text > b.text ? 1 : -1)
But it is sorting numbers first and then letters.
Any help?
If you are looking for a solution like you want to sort string at the first portion of array and numbers at the last of array, just make sure that you are following this procedure.
Never compare a string with a number.
If both a and b are string or both are numbers just compare both with a > b and return 1 or -1 depending on the result of comparision.
The last else block corresponds to the comparision between a number and a string. The return value determines the location of the numbers in the array. If either one of them is not a number, return -1 or 1 depending on the requirement. Since you want the numbers at the end of your array, return -1 if the value is not a number. If you send 1 instead, the numbers will take the first posion in he output array.
const data = [101110 , 124563 , 'France' , 'Austria', 'Germany'];
const output = data.sort((a,b) => {
if (
(isNaN(a) && isNaN(b)) || (!isNaN(a) && !isNaN(b))
) {
// Both are strings
// OR
// Both are numbers
return a > b ? 1 : -1;
}
else {
// One of them is a number
return isNaN(a) ? -1 : 1;
}
});
console.log(output);
Much Simplified version
const data = [101110, 'France', 'Austria', 124563, 'Germany'];
const checkOfSamePattern = (a, b) => (isNaN(a) && isNaN(b)) || (!isNaN(a) && !isNaN(b));
const output = data.sort((a, b) => checkOfSamePattern(a, b) ? a > b ? 1 : -1 : isNaN(a) ? -1 : 1);
console.log(output);
I have the following condition in my angular application
else if (((column.field === "ttUser" 1|| column.field === "ttAdmin") 2&& $scope.EngttAccess) 3|| ((column.field === "btUser" 4|| column.field === "btAdmin") 5&& $scope.EngbtAccess)) {
how can i make this to solve sonar qube issue?
This should behave same way as your example.
else if (
(["ttUser", "ttAdmin"].includes(column.field) && $scope.EngttAccess) ||
(["btUser", "btAdmin"].includes(column.field) && $scope.EngbtAccess)) {
What's a smarter way of doing this? Code works fine but is klunky.
function removeEmptyPending(){
for(var row=2;row<=lastRow;row++){
if( (tkhContact.getRange('A'+row+':A'+row).getValue() == "") &&
(tkhContact.getRange('C'+row+':C'+row).getValue() == "") &&
(tkhContact.getRange('D'+row+':D'+row).getValue() == "") &&
(tkhContact.getRange('E'+row+':E'+row).getValue() == "") &&
(tkhContact.getRange('F'+row+':F'+row).getValue() == "") &&
(tkhContact.getRange('G'+row+':G'+row).getValue() == "") &&
(tkhContact.getRange('H'+row+':H'+row).getValue() == "") &&
(tkhContact.getRange('I'+row+':I'+row).getValue() == "") &&
(tkhContact.getRange('J'+row+':J'+row).getValue() == "") &&
(tkhContact.getRange('K'+row+':K'+row).getValue() == "") &&
(tkhContact.getRange('L'+row+':L'+row).getValue() == "") &&
(tkhContact.getRange('M'+row+':M'+row).getValue() == "") &&
(tkhContact.getRange('N'+row+':N'+row).getValue() == "") &&
(tkhContact.getRange('O'+row+':O'+row).getValue() == "") &&
(tkhContact.getRange('P'+row+':P'+row).getValue() == "") &&
(tkhContact.getRange('Q'+row+':Q'+row).getValue() == "") )
{
tkhContact.deleteRow(row); // tkhContact.getRange('R'+row+':R'+row).setValue("");
}
}
}
I need to skip column B since there is a formula there so it's never really blank.
Use #is_Blank()
function removeEmptyPending(){
for(var row=lastRow;row>=2;row--){
if( (tkhContact.getRange('A'+row+':A'+row).isBlank()) &&
(tkhContact.getRange('C'+row+':Q'+row).isBlank()) &&
){
tkhContact.deleteRow(row); //tkhContact.getRange('R'+row+':R'+row).setValue("");
}
}
}
This code gets all the data from row 2 to the last row, including columns A through Q. And then goes through the data. So, it gets all the data in one operation. The rows need to be deleted individually, and they must be deleted from the bottom up. That means that the outer "for" loop must decrement the counter. If you start deleting from the top down, then the row numbers from the last row deleted, change their row number.
function removeEmptyPending() {
var data,i,j,L,L2,lastRow,row,thisRow,thisValue;
data = tkhContact.getRange(2,1,lastRow-1,17).getValues();//Get all the data from row 2 to the last row
//including column A to column Q
L = data.length;//The number of inner arrays in the outer array
for (i=L;i>0,i--){//Loop through all the rows from last to row 2
thisRow = data[i];//Get one inner array which is one row of data
L2 = thisRow.length;
for (j=0;j<L2,j++){//Loop through each element of the inner array which is the column value
if (j === 1) {continue;} //Skip the second element which is column B
thisValue = thisRow[j];//The value for this column
if (thisValue) {continue;}//The value for this cell is not undefined null or empty string so continue
tkhContact.deleteRow(i+2); // tkhContact.getRange('R'+row+':R'+row).setValue("");
}
}
}
I'm getting a very irritating error whenever I do anything like this with arrays. I have code that sets up the array in the love.load() function:
function iceToolsInit()
objectArray = {} --for object handling
objectArrayLocation = 0
end
and then code that allows for the creation of an object. It basically grabs all of the info about said object and plugs it into an array.
function createObject(x, y, renderimage) --used in the load function
--objectArray is set up in the init function
objectArrayLocation = objectArrayLocation + 1
objectArray[objectArrayLocation] = {}
objectArray[objectArrayLocation]["X"] = x
objectArray[objectArrayLocation]["Y"] = y
objectArray[objectArrayLocation]["renderimage"] =
love.graphics.newImage(renderimage)
end
After this, an update function reads through the objectArray and renders the images accordingly:
function refreshObjects() --made for the update function
arrayLength = #objectArray
arraySearch = 0
while arraySearch <= arrayLength do
arraySearch = arraySearch + 1
renderX = objectArray[arraySearch]["X"]
renderY = objectArray[arraySearch]["Y"]
renderimage = objectArray[arraySearch]["renderimage"]
if movingLeft == true then --rotation for rightfacing images
renderRotation = 120
else
renderRotation = 0
end
love.graphics.draw(renderimage, renderX, renderY, renderRotation)
end
end
I of course clipped some unneeded code (just extra parameters in the array such as width and height) but you get the gist. When I set up this code to make one object and render it, I get this error:
attempt to index '?' (a nil value)
the line it points to is this line:
renderX = objectArray[arraySearch]["X"]
Does anyone know what's wrong here, and how I could prevent it in the future? I really need help with this.
It's off-by-one error:
arraySearch = 0
while arraySearch <= arrayLength do
arraySearch = arraySearch + 1
You run through the loop arrayLength+1 number of times, going through indexes 1..arrayLength+1. You want to go through the loop only arrayLength number of times with indexes 1..arrayLength. The solution is to change the condition to arraySearch < arrayLength.
Another (more Lua-ly way) is to write this as:
for arraySearch = 1, #objectArray do
Even more Lua-ly way is to use ipairs and table.field reference instead of (table["field"]):
function refreshObjects()
for _, el in ipairs(objectArray) do
love.graphics.draw(el.renderimage, el.X, el.Y, movingLeft and 120 or 0)
end
end
objectArray and movingLeft should probably be passed as parameters...
I have a small perl script that needs to evaluate the equality of two parameters and a small return from the database.
my ($firstId, $secondId, $firstReturnedId, $secondReturnedId, $picCount);
my $pics = $dbh->prepare(qq[select id from pictures limit 10]);
$firstId = q->param('firstId');
$secondId = q->param('secondId');
$pics->execute or die;
my $picids = $pics->fetchall_arrayref;
$picCount = scalar(#{$picids});
$firstReturnedId = $picCount > 0 ? shift(#{$picids}) : 0;
$secondReturnedId = $picCount > 1 ? pop(#{$picids}) : $firstReturnedId;
Here, a quick look at my debugger shows that $picCount = 1 and $firstReturnedId = 9020 and $secondReturnedId = 9020. However, they are both denoted as
ARRAY(0x9e79184)
0 9020
in the debugger so when I perform the final check
my $result = (($firstId == $firstReturnedId) && ($secondId == $secondReturnedId)) ? 1 : 0;
I get $result = 0, which is not what I want.
What am I doing wrong?
DBI::fetchall_arrayref returns a reference to a list of "row results". But since there could be more than one value in a row result (e.g., your query could have been select id,other_field from pictures), each row result is also a reference to a list. This means you have one more dereferencing to do in order to get the result you want. Try:
$picCount = scalar(#{$picids});
if ($picCount > 0) {
my $result = shift #{$picids};
$firstReturnedId = $result->[0];
} else {
$firstReturnedId = 0;
}
if ($picCount > 1) {
my $result = pop #{$picids};
$secondReturnedId = $result->[0];
} else {
$secondReturnedId = $firstReturnedId;
}
or if you still want to use a concise style:
$firstReturnedId = $picCount > 0 ? shift(#{$picids})->[0] : 0;
$secondReturnedId = $picCount > 1 ? pop(#{$picids})->[0] : $firstReturnedId;