0

So, I put 4 values in an array and then I sort them... how to avoid values how is not a number to add an array? e.g space,!, and other characters? Then I put the values on different fields by sort...

var points = [t1, t2, t3, t4];
[t1, t2, t3, t4] = points;
points.sort(function(a, b){return a - b});

if (isNaN(t1)==false){this.getField("Text1").value = "§"+points[0]}
if (isNaN(t2)==false){this.getField("Text2").value = "§"+points[1]}
if (isNaN(t3)==false){this.getField("Text3").value = "§"+points[2]}
if (isNaN(t4)==false){this.getField("Text4").value = "§"+points[3]}
tomrlh
  • 1,006
  • 1
  • 18
  • 39
hacker tom
  • 107
  • 1
  • 9
  • Possible duplicate of [Javascript/jQuery: remove all non-numeric values from array](https://stackoverflow.com/questions/24022682/javascript-jquery-remove-all-non-numeric-values-from-array) – imvain2 Feb 13 '19 at 16:39
  • What is happening here: `var points = [t1, t2, t3, t4]; [t1, t2, t3, t4] = points;` – adiga Feb 13 '19 at 17:30

2 Answers2

0

You could parseInt and map the values while checking if isNaN.

Then filter out the undefined. Then sort them like normal

var arr = [01, 23, 55, ' 5', 't'];

var a = arr.map((v, i, ar) => {
    if (!isNaN(parseInt(v))) {
        return parseInt(v)
    }
})

var a = a.filter((el) => {
    return el !== undefined
})

console.log(a.sort((a, b) => a > b))

result = [ 1, 5, 23, 55 ]

djorborn
  • 151
  • 1
  • 7
0

Just apply array filter to your array to filter it content:

points = points.filter(number => !isNaN(number))
.sort(function(a, b){return a - b})
tomrlh
  • 1,006
  • 1
  • 18
  • 39