有一个简单的方法,使嵌套数组平? [英] Is there an easy way to make nested array flat?
本文介绍了有一个简单的方法,使嵌套数组平?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是使这样的:
['狗','猫',['鸡','熊'],['老鼠','马']]
到
['狗','猫','鸡','熊','老鼠','马']
解决方案
VAR扁平= [[0,1],[2,3],[4,5]。减少(功能(A,b){
返回a.concat(B);
});
//压扁为[0,1,2,3,4,5]
这是值得记减少在IE 8不支持和较低的。
<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FArray%2FReduce\">developer.mozilla.org参考
That is to make this:
[ ['dog','cat', ['chicken', 'bear'] ],['mouse','horse'] ]
into:
['dog','cat','chicken','bear','mouse','horse']
解决方案
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
return a.concat(b);
});
// flattened is [0, 1, 2, 3, 4, 5]
It's note worthy that reduce isn't supported in IE 8 and lower.
developer.mozilla.org reference
这篇关于有一个简单的方法,使嵌套数组平?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文