javascript 如何在jQuery中展平数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7875070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to flatten array in jQuery?
提问by extern.fx
How to simply flatten array in jQuery? I have:
如何在 jQuery 中简单地展平数组?我有:
[1, 2, [3, 4], [5, 6], 7]
And I want:
而且我要:
[1, 2, 3, 4, 5, 6, 7]
回答by MarioRicalde
You can use jQuery.map, which is the way to go if you have the jQuery Library already loaded.
您可以使用jQuery.map,如果您已经加载了 jQuery 库,这是一种方法。
$.map( [1, 2, [3, 4], [5, 6], 7], function(n){
return n;
});
Returns
退货
[1, 2, 3, 4, 5, 6, 7]
回答by bjornd
Use the power of JavaScript:
使用 JavaScript 的强大功能:
var a = [[1, 2], 3, [4, 5]];
console.log( Array.prototype.concat.apply([], a) );
//will output [1, 2, 3, 4, 5]
回答by Xavi
Here's how you could use jquery to flatten deeply nested arrays:
以下是如何使用 jquery 来展平深度嵌套的数组:
$.map([1, 2, [3, 4], [5, [6, [7, 8]]]], function recurs(n) {
return ($.isArray(n) ? $.map(n, recurs): n);
});
Returns:
返回:
[1, 2, 3, 4, 5, 6, 7, 8]
Takes advantage of jQuery.mapas well as jQuery.isArray.
回答by dnuttle
var a = [1, 2, [3, 4], [5, [6, [7, 8]]]];
var b = [];
function flatten(e,b){
if(typeof e.length != "undefined")
{
for (var i=0;i<e.length;i++)
{
flatten(e[i],b);
}
}
else
{
b.push(e);
}
}
flatten(a,b);
console.log(b);
The flatten function should do it, and this doesn't require jQuery. Just copy all of this into Firebug and run it.
flatten 函数应该这样做,这不需要 jQuery。只需将所有这些复制到 Firebug 中并运行它。
回答by Fabian Jakobs
To recursively flatten an array you can use the native Array.reducefunction. The is no need to use jQuery for that.
要递归地展平数组,您可以使用本机Array.reduce函数。不需要为此使用 jQuery。
function flatten(arr) {
return arr.reduce(function flatten(res, a) {
Array.isArray(a) ? a.reduce(flatten, res) : res.push(a);
return res;
}, []);
}
Executing
执行
flatten([1, 2, [3, 4, [5, 6]]])
returns
回报
[ 1, 2, 3, 4, 5, 6 ]
回答by Sarfraz
You can use jQuery.map()
:
您可以使用jQuery.map()
:
callback( value, indexOrKey )The function to process each item against. The first argument to the function is the value; the second argument is the index or key of the array or object property. The function can return any value to add to the array. A returned array will be flattened into the resulting array. Within the function, this refers to the global (window) object.
callback( value, indexOrKey )处理每个项目的函数。函数的第一个参数是值;第二个参数是数组或对象属性的索引或键。该函数可以返回任何要添加到数组中的值。返回的数组将被展平为结果数组。在函数中,this 指的是全局(窗口)对象。
回答by Udai Arora
Use recursion if you have multiple levels:
如果您有多个级别,请使用递归:
flaten = function(flatened, arr) {
for(var i=0;i<arr.length;i++) {
if (typeof arr[i]!="object") {
flatened.push(arr[i]);
}
else {
flaten(flatened,arr[i]);
}
}
return;
}
a=[1,[4,2],[2,7,[6,4]],3];
b=[];
flaten(b,a);
console.log(b);
回答by Elise Chant
You can use Array.prototype.reduce
which is technically not jQuery, but valid ES5:
您可以使用Array.prototype.reduce
它在技术上不是 jQuery,而是有效的 ES5:
var multidimensionArray = [1, 2, [3, 4], [5, 6], 7];
var initialValue = [];
var flattened = multidimensionArray.reduce(function(accumulator, current) {
return accumulator.concat(current);
}, initialValue);
console.log(flattened);
回答by Eugen Konkov
You need arr.flat([depth])
var arr1 = [1, 2, [3, 4]];
arr1.flat();
// [1, 2, 3, 4]
var arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]
var arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
// [1, 2, 3, 4, 5, 6]
回答by phil
Old question, I know, but...
老问题,我知道,但是...
I found this works, and is fast:
我发现这有效,而且速度很快:
function flatten (arr) {
b = Array.prototype.concat.apply([], arr);
if (b.length != arr.length) {
b = flatten(b);
};
return b;
}