I want to make dataSet like (11,22) , (11,22,33) , (11,22,33,44) , (11,null,33,44),(11,null,null,44),(11,null,33)
but it should not end with null.
(means the dataSet string should not come (11,22,null,null) like this ).
hpVal=11, outputRpmVal=22,ratioVal=33,outputSpeedVal=44
Assume the above values will come to the arrayPrepareFunction. Anybody could you please help me to make the dataSet string like the above mentioned format?
while making the array set it should not use else if.
The dynamic data will come from user side. i will attach the screen shot. according to the user selection value will be change.

this is the select box order hpVal, outputRpmVal, ratioVal, outputSpeedVal.
For example: if user selects:
(hpVal=11, outputRpmVal=null,ratioVal=33)
the dataset should be like this (11,null,33).
if user selects:
(hpVal=11, outputRpmVal=null,ratioVal=null,outputSpeedVal=44)
the dataset should be like this (11,null,null,44).
if user selects:
(hpVal=11, outputRpmVal=22,ratioVal=null,outputSpeedVal=null)
the dataset should be like this (11,22).
var dataSet,avoid=",null";
function arrayPrepareFunction(hpVal,outputRpmVal,ratioVal,outputSpeedVal){
dataSet="("+hpVal+","+outputRpmVal+","+ratioVal+","+outputSpeedVal+")";
dataSet = dataSet.replace(avoid,'');
if(dataSet.indexOf(avoid) != -1){
dataSet = dataSet.replace(avoid,'');
}
console.log(dataSet);
}
arrayPrepareFunction(11,null,33)
arrayPrepareFunction(11,null,null,44)
arrayPrepareFunction(11,22,null,null)
If you want to construct a string from all the passed arguments and avoid having nullin the last of the string, the best solution would be to treat the inputs as an array, make your check for null items then transform this array into a string.
This is how should be your code:
function arrayPrepareFunction(hpVal, outputRpmVal, ratioVal, outputSpeedVal) {
dataSet = Object.values(arguments);
var i = dataSet.length;
while (dataSet[i - 1] == null && i > 0) {
dataSet.pop();
i--;
}
dataSet = "(" + dataSet.map(a => a == null ? "" + a : a).join(", ") + ")";
console.log(dataSet);
}
Object.values(arguments) to get the input arguments
as array. array if it is null.array into a string.Demo:
var dataSet, avoid = ",null";
function arrayPrepareFunction(hpVal, outputRpmVal, ratioVal, outputSpeedVal) {
dataSet = Object.values(arguments);
var i = dataSet.length;
while (dataSet[i - 1] == null && i > 0) {
dataSet.pop();
i--;
}
dataSet = "(" + dataSet.map(a => a == null ? "" + a : a).join(", ") + ")";
console.log(dataSet);
}
arrayPrepareFunction(11, null, 33)
arrayPrepareFunction(11, null, null, 44)
arrayPrepareFunction(11, 22, null, null)
Not sure if this is the best answer, but it seams to work:
This puts all the elements into an array >> removes the last few elements if null >> format
function arrayPrepareFunction(hpVal,outputRpmVal,ratioVal,outputSpeedVal){
let arr = [hpVal, outputRpmVal, ratioVal, outputSpeedVal];
for(let i = arr.length-1; i >= 0; i --){
if(arr[i] == null) {
arr.pop();
}
else {
break;
}
}
let dataSet = '('
for(let i = 0; i < arr.length; i ++) {
dataSet += arr[i] + (i != arr.length - 1? ',': '');
}
dataSet += ')';
console.log(dataSet);
}
arrayPrepareFunction(11,null,33);
arrayPrepareFunction(11,null,null,44);
arrayPrepareFunction(11,22,null,null);
Outputs:
(11,null,33)
(11,null,null,44)
(11,22)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With