尝试这个:
function getValuesFrom(array,x)
{
x = +(x);//coerce to number
array.sort(function(a,b)
{
return (a > b ? 1 : -1);
});//sort in ascending order
for (var i = 0;i<array.length;i++)
{
if (+array[i] >= x)
{
return array.slice(i);//return all elements >= x
}
}
}
var myArray = [10,11,12,13,14,15,16,17,18,19];
var larger = getValuesFrom(myArray,15);
console.log(larger);//[15,16,17,18,19]
console.log(larger.length);//5
那应该这样做。注意:将数组传递给此函数后,您可能会发现它的顺序发生了变化,以避免这种情况:
function getValuesFrom(array,x)
{
x = +(x);//coerce to number
var tmpArray = array.slice(0);//copy array
tmpArray.sort(function(a,b)
{
return (a > b ? 1 : -1);
});//sort in ascending order
for (var i = 0;i<tmpArray.length;i++)
{
if (+tmpArray[i] >= x)
{
return tmpArray.slice(i);//return all elements >= x
}
}
}