- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2022 09:12 PM
I have a requirement to get minimum value from the one field in a table
field name : case status
type : string
value : 10
Need :
input - ["10","10","10","10","1","6","10","3","10","3","10","10","10","10","6","1","10","10"]
output - minimum value = 1
maximum value = 10
Method i have tried but not working
getting the value in format of 10 as string. can't able to get min value by using script include i got value in array like ["10","10","10","10","1","6","10","3","10","3","10","10","10","10","6","1","10","10"]
- i have try sort() method but it is not sorting properly because of ASCII value
var a = ["10","10","10","10","1","6","10","3","10","3","10","10","10","10","6","1","10","10"]
a.sort(); - tried loop method to get highest number and smallest numberfor(var j=0;j<sepVal.length;j++){if(min < sepVal[j]);min=sepVal[j];}
this method is also not working - used Math.min.apply(null,array)
it is also not working
please help me to find answer
thank you in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2022 09:28 PM - edited 10-06-2022 09:29 PM
var a= ["10","10","10","10","1","6","10","3","10","3","10","10","10","10","6","1","10","10"];
var max = parseInt(a[0]);
var min = parseInt(a[0]);
for( var i in a)
{
if(parseInt(a[i]) > max)
{
max = parseInt(a[i]);
}
if(parseInt(a[i]) < min)
{
min =parseInt(a[i]);
}
}
gs.print(max+","+min);
Mark Correct and Helpful.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2022 10:44 PM - edited 10-06-2022 10:46 PM
Hi Mohan,
Please use this below simple script, it will work.
var arr = ["1", "2", "3"];
// array of strings
var nums = arr.map(function(str) {
// using map() to convert array of strings to numbers
return parseInt(str); });
gs.info(nums);
gs.info(Math.max.apply(Math, nums));
gs.info(Math.min.apply(Math, nums));
//OUTPUT:
//*** Script: 1,2,3
//*** Script: 3
//*** Script: 1
Please mark the answer correct once it helps you. 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2022 09:28 PM - edited 10-06-2022 09:29 PM
var a= ["10","10","10","10","1","6","10","3","10","3","10","10","10","10","6","1","10","10"];
var max = parseInt(a[0]);
var min = parseInt(a[0]);
for( var i in a)
{
if(parseInt(a[i]) > max)
{
max = parseInt(a[i]);
}
if(parseInt(a[i]) < min)
{
min =parseInt(a[i]);
}
}
gs.print(max+","+min);
Mark Correct and Helpful.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2022 10:44 PM - edited 10-06-2022 10:46 PM
Hi Mohan,
Please use this below simple script, it will work.
var arr = ["1", "2", "3"];
// array of strings
var nums = arr.map(function(str) {
// using map() to convert array of strings to numbers
return parseInt(str); });
gs.info(nums);
gs.info(Math.max.apply(Math, nums));
gs.info(Math.min.apply(Math, nums));
//OUTPUT:
//*** Script: 1,2,3
//*** Script: 3
//*** Script: 1
Please mark the answer correct once it helps you. 🙂