Can I Get The Object Name in A Loop? - Swift 2 - arrays

I've written this loop. But, I'm wondering if I can do without the name property and just reference the name of the object?
class Param {
var name = ""
var value = 0.0
var skip = -1
}
let depth = Param()
depth.name = "depth"
let tankPressure = Param()
tankPressure.name = "tankPressure"
let time = Param()
time.name = "time"
let tankCapacity = Param()
tankCapacity.name = "tankCapacity"
let litresPerMinute = Param()
litresPerMinute.name = "litresPerMinute"
let params = [depth, tankPressure, time, tankCapacity, litresPerMinute]
for param in params {
let outlet = "\(param.name)Outlet.text!"
print(outlet)
}

Related

Pushing Distinct Values to Array and Finding New Length

Trying to sort and push only the distinct values into a new array and then find the length of the resulting array. See code below. The UniqueTeams.length is coming out to be 1, when it should be 5 (I thought).
var teams = [[formSS.getRange("F8").getValue(),
formSS.getRange("F10").getValue(),
formSS.getRange("F12").getValue(),
formSS.getRange("F14").getValue(),
formSS.getRange("F16").getValue()]];
var uniqueTeams = removeDups(teams);
if(uniqueTeams.length < 5){
{SpreadsheetApp.getUi().alert(uniqueTeams);
return;}
}
function removeDups(array) {
var outArray = [];
array.sort();
outArray.push(array[0]);
for(var n in array){
if(outArray[outArray.length-1]!=array[n]){
outArray.push(array[n]);
}
}
return outArray;
}
uniqueTeams = Alabama (2),Maryland (10),Cleveland St (15),BYU (6),Liberty (13)
uniqueTeams.length = 1
Get unique elements
function remdup() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const t = sh.getRange('F8:F16').getValues().flat();
let teams = [t[0], t[2], t[4], t[6], t[8]];
let s = new Set(teams)
teams = [...s];
Logger.log(teams.join(','))
}
Set
Cooper's answer it the best ultimate solution. But if you want just to fix your code you can try to change the line:
var uniqueTeams = removeDups(teams);
with:
var uniqueTeams = removeDups(teams[0]);
Since the teams is a 2D array [[...]].

Efficient way to use display the results from the fetch?

I am started learning reactjs. I have fetch result from the given API https://lichess.org/api/user/nihalsarin2004 which shows the profile of Lichess User. (Here nihalsarin2004). But for using every detail, I have coded them as below:
let fName = JSON.stringify(data?.profile?.firstName);
let lName = JSON.stringify(data?.profile?.lastName);
let totalGames = JSON.stringify(data?.count?.all);
let totalWins = JSON.stringify(data?.count?.win);
let totalDraws = JSON.stringify(data?.count?.draw);
let totalLoss = JSON.stringify(data?.count?.loss);
let userLink = JSON.stringify(data?.url);
.
.
.
.
.
let blitzRating = JSON.stringify(data?.perfs?.blitz?.rating);
let bulletRating = JSON.stringify(data?.perfs?.bullet?.rating);
let rapidRating = JSON.stringify(data?.perfs?.rapid?.rating);
let classicalRating = JSON.stringify(data?.perfs?.classical?.rating);
let chess960Rating = JSON.stringify(data?.perfs?.chess960?.rating);
let antichessRating = JSON.stringify(data?.perfs?.antichess?.rating);
let checkRating = JSON.stringify(data?.perfs?.threeCheck?.rating);
let atomicRating = JSON.stringify(data?.perfs?.atomic?.rating);
let hordeRating = JSON.stringify(data?.perfs?.horde?.rating);
And then using these variables in react components ?
Do we any alternative for this ?
Use Destructuring Assignment and assign a new variable names in the destructuring:
let { firstName: fName, lastName: lName } = data?.profile;
let { all: totalGames, win: totalWins /* and so on */ } = data?.count;
You can de-structure them as follows:
if(!data.profile){
// check if there is data and profile
// same for other keys in data obj
return null
}
let {firstName, lastName} = data.profile
let {all,win, draw, loss} = data.count
// so on...
Note: whenever you are de-structuring any variable from an object make sure you name that variable same as key that you want to extract
for example:
let obj={
name:"John",
age:20,
}
// if you want to destructure obj you can do following
let {name, age}=obj
//variable names in {} should be same as key in obj

Array Map multidimensional, get values from 3 array Java

Let say I have 3 arrays,
compCD = ["909", "908", "080", "901"];
contNO = ["09999", "08888", "00777", "00666"];
pomNum = ["A01", "A02", "A03", "A04"];
How can I insert into jsonMap each separated Values like
[
{
"compCD" = "909",
"contNO" = "09999",
"pomNum" = "A01"
},{
"compCD" = "908",
"contNO" = "08888",
"pomNum" = "A02"
}
]
we can assume that each array size is the same. The first of each array will bi insert first to map.
How we can solve those with minimum loop.
var compCD = ["909", "908", "080", "901"];
var contNO = ["09999", "08888", "00777", "00666"];
var pomNum = ["A01", "A02", "A03", "A04"];
var jsonMap=[];
for(var i=0; i<compCD.length; i++) {
jsonMap.push(
{compCD:compCD[i], contNO:contNO[i], pomNum:pomNum[i]}
);
};
console.log(jsonMap);
var compCD = ["909", "908", "080", "901"];
var contNO = ["09999", "08888", "00777", "00666"];
var pomNum = ["A01", "A02", "A03", "A04"];
var jsonMap=compCD.map(
(currentValue, index)=>[currentValue, contNO[index], pomNum[index]]
);
console.log(jsonMap);

How to sort array of custom objects by customised status value in swift 3?

lets say we have a custom class named orderFile and this class contains three properties.
class orderFile {
var name = String()
var id = Int()
var status = String()
}
a lot of them stored into an array
var aOrders : Array = []
var aOrder = orderFile()
aOrder.name = "Order 1"
aOrder.id = 101
aOrder.status = "closed"
aOrders.append(aOrder)
var aOrder = orderFile()
aOrder.name = "Order 2"
aOrder.id = 101
aOrder.status = "open"
aOrders.append(aOrder)
var aOrder = orderFile()
aOrder.name = "Order 2"
aOrder.id = 101
aOrder.status = "cancelled"
aOrders.append(aOrder)
var aOrder = orderFile()
aOrder.name = "Order 2"
aOrder.id = 101
aOrder.status = "confirmed"
aOrders.append(aOrder)
Question is: How will I sort them based on status according to open, confirm, close and cancelled?
You have to provide a value that will yield the appropriate ordering when compared in the sort function.
For example:
extension orderFile
{
var statusSortOrder: Int
{ return ["open","confirmed","closed","cancelled"].index(of: status) ?? 0 }
}
let sortedOrders = aOrders.sorted{$0.statusSortOrder < $1. statusSortOrder}
In your code you should make an array to store each aOrder with aOrders.append(aOrder) at each aOrder defination.
Then sort it with below code, refor this for more.
aOrders.sorted({ $0.status > $1.status })
The answer for swift 3 is as following
Ascending:
aOrders = aOrders.sorted(by:
{(first: orderFile, second: orderFile) -> Bool in
first.status > second.status
}
)
Descending:
aOrders = aOrders.sorted(by:
{(first: orderFile, second: orderFile) -> Bool in
first.status < second.status
}
)

Update List with Loop Arguments Scala

I'm trying create new random List based on other List. But this code doesn't work to update the var
import scala.util.Random
import scala.math
val randSymbol = List(1,2,2,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,8)
val _finalSymbol = List(List(0,0,0,0,0),List(0,0,0,0,0),List(0,0,0,0,0))
var i:Int = 0
var a:Int = 0
for (i <- 0 to 2){
_finalSymbol(i) = new List
for (a <- 0 to 4){
var iRandIndex = floor(Random.nextInt() * randSymbol.length).toInt
var iRandSymbol = randSymbol(iRandIndex)
_finalSymbol(i)(a) = iRandSymbol
}
}
Try something like this:
val _finalSymbol = (0 to 2) map { _ => Random.shuffle(randSymbol).take(5) }
And do yourself a favor: buy a scala book.
It's not javascript. At all.

Resources