将多个数组的数据根据下标放入新的数组中(通俗来说就是每个数组的同样下标的数据组成一个新的数组)
例如:
0: (6) [26.67, 35, 51.67, '', 35, 42.5]
1: (6) ['', '', '', '', 35, 42.5]
2: (6) ['', '', '', '', 35, 42.5]
3: (6) [26.67, 35, 51.67, 40, 35, 42.5]
4: (6) [26.67, 35, 51.67, 40, 35, 42.5]
5: (6) ['', '', '', '', 35, 42.5]
6: (6) ['', '', '', '', 35, 42.5]
7: (6) [26.67, 35, 51.67, 40, '', '']
8: (6) [26.67, 35, 51.67, 40, '', '']
9: (6) [26.67, 35, 51.67, 40, '', '']
解析后:
0: (10) [26.67, '', '', 26.67, 26.67, '', '', 26.67, 26.67, 26.67]
1: (10) [35, '', '', 35, 35, '', '', 35, 35, 35]
2: (10) [51.67, '', '', 51.67, 51.67, '', '', 51.67, 51.67, 51.67]
3: (10) ['', '', '', 40, 40, '', '', 40, 40, 40]
4: (10) [35, 35, 35, 35, 35, 35, 35, '', '', '']
5: (10) [42.5, 42.5, 42.5, 42.5, 42.5, 42.5, 42.5, '', '', '']
代码实现:
function merge(arrs, name) {
//arrs是需要解析的数据,name是新数组的名称
var maxLen = Math.max(...arrs.map(x => x.length))
var result = []
for (let i = 0; i < maxLen; i++) {
result.push(arrs.filter(x => x.length > i).map(x => x[i]))
}
name.push(result)
return result
}
merge(fenshuA, arrShuzu)
数据去空
var r = arrShuzu[0][m].filter(function(s) {
return s && $.trim(s); // 注:IE9(不包含IE9)以下的版本没有trim()方法
});
有的时候直接使用s.trim()
方法会报错,也可以使用$.trim(s)