Clean undefined items in Numeric Array and sort it by javascript -


i have array created inside for loops

below array of numbers contains undefined items & dont know right way remove undefined items , sort finely , see:

2426,3045,,1680,,,1323,,,,1311 

after sorting , here first 2 items merged :

24263045,1680,,1323,,,1311,,,, 

here code:

var textcontent = 'larg text content larg text content larg text content larg text content larg text content larg text content'; var words = 'content larg text'; var word = words.split(' ');  for(var i=0;i<word.length;i++){ var kx=[]; kx[i] = textcontent.indexof(word[i]);  function sortnumber(a,b) { return - b; } //trying sort var vk = kx.sort(sortnumber); document.write(vk); // returns // 24263045,1680,,1323,,,1311,,,,  } 

how remove undefined items , sort be

1311,1323,1680,2426,3045 

you can make use of filter() .

var arr = [2426,3045,,1680,,,1323,,,,1311]; function test(array){     var arr = array.filter(function(item){       return item && item!='';//check if string item empty.     })     console.log(arr);     return arr; } //call var arr = test(arr).sort();//to sort 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -