[英]Clean undefined items in Numeric Array and sort it by javascript
i have an array created inside for loops
我在for loops
创建了一个数组
below is an array of numbers contains undefined items & i dont know the right way to remove undefined items and sort it finely , see: 以下是包含未定义项目的数字数组,我不知道删除未定义项目并对其进行精细排序的正确方法,请参见:
2426,3045,,1680,,,1323,,,,1311
after sorting , here is first two items merged : 排序后,这是前两项合并:
24263045,1680,,1323,,,1311,,,,
Here is my 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 a - b;
}
//trying to sort that
var vk = kx.sort(sortNumber);
document.write(vk);
// it returns something like this
// 24263045,1680,,1323,,,1311,,,,
}
How to remove undefined items and sort it to be 如何删除未定义的项目并对其进行排序
1311,1323,1680,2426,3045
You can make use of filter() . 您可以使用filter() 。
var arr = [2426,3045,,1680,,,1323,,,,1311];
function test(array){
var arr = array.filter(function(item){
return item && item!='';//Check if string item is empty.
})
console.log(arr);
return arr;
}
//Call
var arr = test(arr).sort();//To sort
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.