Convert array contain string ["1",2",3"] into number [1,2,3]

Mohan6
Tera Contributor

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"]

  1. 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();

  2. tried loop method to get highest number and smallest number
    for(var j=0;j<sepVal.length;j++){
        if(min < sepVal[j]);
        min=sepVal[j];
    }
    this method is also not working 

     
  3. used Math.min.apply(null,array) 
    it is also not working 

    please help me to find answer
    thank you in advance
2 ACCEPTED SOLUTIONS

Saiganeshraja
Kilo Sage
Kilo Sage

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.

View solution in original post

H_9
Giga Guru

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. 🙂

Thanks. 🙂

View solution in original post

2 REPLIES 2

Saiganeshraja
Kilo Sage
Kilo Sage

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.

H_9
Giga Guru

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. 🙂

Thanks. 🙂