And*_*ndy 1001 javascript arrays flatten
我有一个JavaScript数组,如:
[["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]]
Run Code Online (Sandbox Code Playgroud)
我将如何将单独的内部数组合并为:
["$6", "$12", "$25", ...]
Run Code Online (Sandbox Code Playgroud)
Gum*_*mbo 1713
您可以使用concat合并数组:
var arrays = [
["$6"],
["$12"],
["$25"],
["$25"],
["$18"],
["$22"],
["$10"]
];
var merged = [].concat.apply([], arrays);
console.log(merged);Run Code Online (Sandbox Code Playgroud)
使用apply方法concat只会将第二个参数作为数组,所以最后一行与此相同:
var merged2 = [].concat(["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]);
Run Code Online (Sandbox Code Playgroud)
还有一种实验Array.prototype.flat()方法(尚未成为ECMAScript标准的一部分),您可以使用它来展平阵列,尽管它在Edge或Node.js中尚不可用.
const arrays = [
["$6"],
["$12"],
["$25"],
["$25"],
["$18"],
["$22"],
["$10"]
];
const merge3 = arrays.flat(1); //The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
console.log(merge3);
Run Code Online (Sandbox Code Playgroud)
Noa*_*tas 466
这是一个简短的函数,它使用一些较新的JavaScript数组方法来展平n维数组.
function flatten(arr) {
return arr.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
}, []);
}
Run Code Online (Sandbox Code Playgroud)
用法:
flatten([[1, 2, 3], [4, 5]]); // [1, 2, 3, 4, 5]
flatten([[[1, [1.1]], 2, 3], [4, 5]]); // [1, 1.1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
Nik*_*kov 305
这是一个简单而高效的功能解决方案:
var oldArray = [[1],[2,3],[4]];
var newArray = Array.prototype.concat.apply([], oldArray);
console.log(newArray); // [ 1, 2, 3, 4 ]Run Code Online (Sandbox Code Playgroud)
没有必要的混乱.
小智 183
最好通过javascript reduce函数来完成.
var arrays = [["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"], ["$0"], ["$15"],["$3"], ["$75"], ["$5"], ["$100"], ["$7"], ["$3"], ["$75"], ["$5"]];
arrays = arrays.reduce(function(a, b){
return a.concat(b);
}, []);
Run Code Online (Sandbox Code Playgroud)
或者,使用ES2015:
arrays = arrays.reduce((a, b) => a.concat(b), []);
Run Code Online (Sandbox Code Playgroud)
Mic*_*ski 71
这里的大多数答案不适用于巨大的(例如200 000个元素)阵列,即使它们这样做,它们也很慢.polkovnikov.ph的答案具有最佳性能,但它不适用于深度扁平化.
这是最快的解决方案,它也适用于具有多级嵌套的数组:
const flatten = function(arr, result = []) {
for (let i = 0, length = arr.length; i < length; i++) {
const value = arr[i];
if (Array.isArray(value)) {
flatten(value, result);
} else {
result.push(value);
}
}
return result;
};
Run Code Online (Sandbox Code Playgroud)
flatten(Array(200000).fill([1]));
Run Code Online (Sandbox Code Playgroud)
它处理大型数组就好了.在我的机器上,此代码执行大约需要14毫秒.
flatten(Array(2).fill(Array(2).fill(Array(2).fill([1]))));
Run Code Online (Sandbox Code Playgroud)
它适用于嵌套数组.这段代码产生了[1, 1, 1, 1, 1, 1, 1, 1].
flatten([1, [1], [[1]]]);
Run Code Online (Sandbox Code Playgroud)
像这样的扁平化数组没有任何问题.
Ali*_*ter 64
有一个名为flat的新的原生ECMA 2018方法可以完全做到这一点.
const arr1 = [1, 2, [3, 4]];
arr1.flat();
// [1, 2, 3, 4]
const arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]
// Flatten 2 levels deep
const arr3 = [2, 2, 5, [5, [5, [6]], 7]];
arr3.flat(2);
// [2, 2, 5, 5, 5, [6], 7];
// Flatten all levels
const arr4 = [2, 2, 5, [5, [5, [6]], 7]];
arr4.flat(Infinity);
// [2, 2, 5, 5, 5, 6, 7];
Run Code Online (Sandbox Code Playgroud)
Mic*_*ski 54
更新:事实证明,此解决方案不适用于大型阵列.您正在寻找更好,更快的解决方案,请查看此答案.
function flatten(arr) {
return [].concat(...arr)
}
Run Code Online (Sandbox Code Playgroud)
只是扩展arr并将其作为参数传递给它concat(),它将所有数组合并为一个.它相当于[].concat.apply([], arr).
你也可以试试深度展平:
function deepFlatten(arr) {
return flatten( // return shalowly flattened array
arr.map(x=> // with each x in array
Array.isArray(x) // is x an array?
? deepFlatten(x) // if yes, return deeply flattened x
: x // if no, return just x
)
)
}
Run Code Online (Sandbox Code Playgroud)
请参阅JSBin上的演示.
本答案中使用的ECMAScript 6元素的参考:
附注:find()所有浏览器都不支持类似和箭头功能的方法,但这并不意味着您现在无法使用这些功能.只需使用Babel - 它将ES6代码转换为ES5.
Tod*_*ell 50
你可以使用下划线:
var x = [[1], [2], [3, 4]];
_.flatten(x); // => [1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
Tha*_*you 41
通用程序意味着每次我们需要利用特定行为时,我们不必重写复杂性.
concatMap(或flatMap)正是我们在这种情况下所需要的.
// concat :: ([a],[a]) -> [a]
const concat = (xs,ys) =>
xs.concat (ys)
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = f => xs =>
xs.map(f).reduce(concat, [])
// id :: a -> a
const id = x =>
x
// flatten :: [[a]] -> [a]
const flatten =
concatMap (id)
// your sample data
const data =
[["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]]
console.log (flatten (data))Run Code Online (Sandbox Code Playgroud)
是的,你猜对了,它只会使一个级别变平,这正是它应该如何运作的
想象一下像这样的一些数据集
// Player :: (String, Number) -> Player
const Player = (name,number) =>
[ name, number ]
// team :: ( . Player) -> Team
const Team = (...players) =>
players
// Game :: (Team, Team) -> Game
const Game = (teamA, teamB) =>
[ teamA, teamB ]
// sample data
const teamA =
Team (Player ('bob', 5), Player ('alice', 6))
const teamB =
Team (Player ('ricky', 4), Player ('julian', 2))
const game =
Game (teamA, teamB)
console.log (game)
// [ [ [ 'bob', 5 ], [ 'alice', 6 ] ],
// [ [ 'ricky', 4 ], [ 'julian', 2 ] ] ]Run Code Online (Sandbox Code Playgroud)
好的,现在说我们要打印一个名单,显示所有参与的球员game......
const gamePlayers = game =>
flatten (game)
gamePlayers (game)
// => [ [ 'bob', 5 ], [ 'alice', 6 ], [ 'ricky', 4 ], [ 'julian', 2 ] ]
Run Code Online (Sandbox Code Playgroud)
如果我们的flatten程序也使嵌套数组变平,我们最终会得到这个垃圾结果......
const gamePlayers = game =>
badGenericFlatten(game)
gamePlayers (game)
// => [ 'bob', 5, 'alice', 6, 'ricky', 4, 'julian', 2 ]
Run Code Online (Sandbox Code Playgroud)
这并不是说有时候你也不想整理嵌套数组 - 只是不应该是默认行为.
我们可以轻松制作deepFlatten程序......
// concat :: ([a],[a]) -> [a]
const concat = (xs,ys) =>
xs.concat (ys)
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = f => xs =>
xs.map(f).reduce(concat, [])
// id :: a -> a
const id = x =>
x
// flatten :: [[a]] -> [a]
const flatten =
concatMap (id)
// deepFlatten :: [[a]] -> [a]
const deepFlatten =
concatMap (x =>
Array.isArray (x) ? deepFlatten (x) : x)
// your sample data
const data =
[0, [1, [2, [3, [4, 5], 6]]], [7, [8]], 9]
console.log (flatten (data))
// [ 0, 1, [ 2, [ 3, [ 4, 5 ], 6 ] ], 7, [ 8 ], 9 ]
console.log (deepFlatten (data))
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]Run Code Online (Sandbox Code Playgroud)
那里.现在,每个作业都有一个工具 - 一个用于压缩一个嵌套级别flatten,另一个用于清除所有嵌套deepFlatten.
也许你可以打电话obliterate或者nuke你不喜欢这个名字deepFlatten.
不要迭代两次!
当然上面的实现是聪明和简洁的,但是使用.map后跟调用.reduce意味着我们实际上做了比必要更多的迭代
使用可信赖的组合器,我正在调用mapReduce有助于将迭代保持为minium; 它需要一个映射功能m :: a -> b,一个还原功能r :: (b,a) ->b并返回一个新的还原功能 - 这个组合器是传感器的核心; 如果你有兴趣,我会写下关于他们的其他答案
// mapReduce = (a -> b, (b,a) -> b, (b,a) -> b)
const mapReduce = (m,r) =>
(acc,x) => r (acc, m (x))
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = f => xs =>
xs.reduce (mapReduce (f, concat), [])
// concat :: ([a],[a]) -> [a]
const concat = (xs,ys) =>
xs.concat (ys)
// id :: a -> a
const id = x =>
x
// flatten :: [[a]] -> [a]
const flatten =
concatMap (id)
// deepFlatten :: [[a]] -> [a]
const deepFlatten =
concatMap (x =>
Array.isArray (x) ? deepFlatten (x) : x)
// your sample data
const data =
[ [ [ 1, 2 ],
[ 3, 4 ] ],
[ [ 5, 6 ],
[ 7, 8 ] ] ]
console.log (flatten (data))
// [ [ 1. 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ]
console.log (deepFlatten (data))
// [ 1, 2, 3, 4, 5, 6, 7, 8 ]Run Code Online (Sandbox Code Playgroud)
Tri*_*daz 32
对于更一般的情况的解决方案,当您的数组中可能有一些非数组元素时.
function flattenArrayOfArrays(a, r){
if(!r){ r = []}
for(var i=0; i<a.length; i++){
if(a[i].constructor == Array){
r.concat(flattenArrayOfArrays(a[i], r));
}else{
r.push(a[i]);
}
}
return r;
}
Run Code Online (Sandbox Code Playgroud)
diz*_*iaq 29
功能风格的另一个ECMAScript 6解决方案:
声明功能:
const flatten = arr => arr.reduce(
(a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []
);
Run Code Online (Sandbox Code Playgroud)
并使用它:
flatten( [1, [2,3], [4,[5,[6]]]] ) // -> [1,2,3,4,5,6]
Run Code Online (Sandbox Code Playgroud)
更新:
还要考虑现代浏览器的最新版本中提供的本机函数Array.prototype.flat()(ES6的提议).
感谢@(КонстантинВан)和@(Mark Amery)在评论中提及它.
rab*_*rab 27
怎么用的reduce(callback[, initialValue])方法JavaScript 1.8
list.reduce((p,n) => p.concat(n),[]);
Run Code Online (Sandbox Code Playgroud)
会做的工作.
Den*_*ret 25
要展平单个元素数组的数组,您不需要导入库,简单循环是最简单和最有效的解决方案:
for (var i = 0; i < a.length; i++) {
a[i] = a[i][0];
}
Run Code Online (Sandbox Code Playgroud)
对于downvoters:请阅读问题,不要downvote,因为它不适合你非常不同的问题.对于问题,这个解决方案既是最快也最简单的.
Wil*_*een 22
您也可以尝试新Array.flat()方法。它以下列方式工作:
let arr = [["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]].flat()
console.log(arr);Run Code Online (Sandbox Code Playgroud)
该flat()方法创建一个新数组,其中所有子数组元素以递归方式连接到其中,直到 1 层深度(即数组内的数组)
如果您还想展平 3 维或什至更高维的数组,您只需多次调用 flat 方法。例如(3维):
let arr = [1,2,[3,4,[5,6]]].flat().flat().flat();
console.log(arr);Run Code Online (Sandbox Code Playgroud)
Array.flat()方法比较新。像 ie 这样的旧浏览器可能没有实现该方法。如果你想让你的代码在所有浏览器上工作,你可能必须将你的 JS 转换成旧版本。检查MDN 网络文档以了解当前浏览器的兼容性。
Yai*_*wil 20
const common = arr.reduce((a, b) => [...a, ...b], [])
Run Code Online (Sandbox Code Playgroud)
小智 14
请注意:当使用Function.prototype.apply([].concat.apply([], arrays))或扩展运算符([].concat(...arrays))来展平数组时,两者都会导致大型数组的堆栈溢出,因为函数的每个参数都存储在堆栈中.
这是一个功能样式的堆栈安全实现,它重叠了彼此最重要的要求:
// small, reusable auxiliary functions:
const foldl = f => acc => xs => xs.reduce(uncurry(f), acc); // aka reduce
const uncurry = f => (a, b) => f(a) (b);
const concat = xs => y => xs.concat(y);
// the actual function to flatten an array - a self-explanatory one-line:
const flatten = xs => foldl(concat) ([]) (xs);
// arbitrary array sizes (until the heap blows up :D)
const xs = [[1,2,3],[4,5,6],[7,8,9]];
console.log(flatten(xs));
// Deriving a recursive solution for deeply nested arrays is trivially now
// yet more small, reusable auxiliary functions:
const map = f => xs => xs.map(apply(f));
const apply = f => a => f(a);
const isArray = Array.isArray;
// the derived recursive function:
const flattenr = xs => flatten(map(x => isArray(x) ? flattenr(x) : x) (xs));
const ys = [1,[2,[3,[4,[5],6,],7],8],9];
console.log(flattenr(ys));Run Code Online (Sandbox Code Playgroud)
一旦习惯了咖喱形式,函数组合和高阶函数的小箭头函数,这段代码就像散文一样.编程然后仅仅包括总是按预期工作的小构建块,因为它们不包含任何副作用.
Cat*_*ish 11
使用扩展运算符:
const input = [["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]];
const output = [].concat(...input);
console.log(output); // --> ["$6", "$12", "$25", "$25", "$18", "$22", "$10"]Run Code Online (Sandbox Code Playgroud)
zur*_*fyx 10
见lodash flatten,undercore flatten (浅true)
function flatten(arr) {
return arr.reduce((acc, e) => acc.concat(e), []);
}
Run Code Online (Sandbox Code Playgroud)
function flatten(arr) {
return [].concat.apply([], arr);
}
Run Code Online (Sandbox Code Playgroud)
经过测试
test('already flatted', () => {
expect(flatten([1, 2, 3, 4, 5])).toEqual([1, 2, 3, 4, 5]);
});
test('flats first level', () => {
expect(flatten([1, [2, [3, [4]], 5]])).toEqual([1, 2, [3, [4]], 5]);
});
Run Code Online (Sandbox Code Playgroud)
function flattenDeep(arr) {
return arr.reduce((acc, e) => Array.isArray(e) ? acc.concat(flattenDeep(e)) : acc.concat(e), []);
}
Run Code Online (Sandbox Code Playgroud)
经过测试
test('already flatted', () => {
expect(flattenDeep([1, 2, 3, 4, 5])).toEqual([1, 2, 3, 4, 5]);
});
test('flats', () => {
expect(flattenDeep([1, [2, [3, [4]], 5]])).toEqual([1, 2, 3, 4, 5]);
});
Run Code Online (Sandbox Code Playgroud)
您可以将Array.flat()with Infinity用于任何深度的嵌套数组。
var arr = [ [1,2,3,4], [1,2,[1,2,3]], [1,2,3,4,5,[1,2,3,4,[1,2,3,4]]], [[1,2,3,4], [1,2,[1,2,3]], [1,2,3,4,5,[1,2,3,4,[1,2,3,4]]]] ];
let flatten = arr.flat(Infinity)
console.log(flatten)Run Code Online (Sandbox Code Playgroud)
在此处查看浏览器兼容性
ES6方式:
const flatten = arr => arr.reduce((acc, next) => acc.concat(Array.isArray(next) ? flatten(next) : next), [])
const a = [1, [2, [3, [4, [5]]]]]
console.log(flatten(a))Run Code Online (Sandbox Code Playgroud)
ES5方式用于flattenN次嵌套数组的ES3回退功能:
var flatten = (function() {
if (!!Array.prototype.reduce && !!Array.isArray) {
return function(array) {
return array.reduce(function(prev, next) {
return prev.concat(Array.isArray(next) ? flatten(next) : next);
}, []);
};
} else {
return function(array) {
var arr = [];
var i = 0;
var len = array.length;
var target;
for (; i < len; i++) {
target = array[i];
arr = arr.concat(
(Object.prototype.toString.call(target) === '[object Array]') ? flatten(target) : target
);
}
return arr;
};
}
}());
var a = [1, [2, [3, [4, [5]]]]];
console.log(flatten(a));Run Code Online (Sandbox Code Playgroud)
一种Haskellesque方法
function flatArray([x,...xs]){
return x ? [...Array.isArray(x) ? flatArray(x) : [x], ...flatArray(xs)] : [];
}
var na = [[1,2],[3,[4,5]],[6,7,[[[8],9]]],10];
fa = flatArray(na);
console.log(fa);Run Code Online (Sandbox Code Playgroud)
如果你使用lodash,你可以使用它的flatten方法:https ://lodash.com/docs/4.17.14#flatten
lodash 的好处是它还有压平数组的方法:
i)递归地:https://lodash.com/docs/4.17.14#flattenDeep
ii) 最多 n 层嵌套:https://lodash.com/docs/4.17.14#flattenDepth
例如
const _ = require("lodash");
const pancake = _.flatten(array)
Run Code Online (Sandbox Code Playgroud)
如果只有1个字符串元素的数组:
[["$6"], ["$12"], ["$25"], ["$25"]].join(',').split(',');
Run Code Online (Sandbox Code Playgroud)
会做的.Bt特别匹配您的代码示例.
var arrays = [["a"], ["b", "c"]];
Array.prototype.concat.apply([], arrays);
// gives ["a", "b", "c"]
Run Code Online (Sandbox Code Playgroud)
(我只是根据@danhbear的评论将其作为一个单独的答案.)
我推荐一个节省空间的发电机功能:
function* flatten(arr) {
if (!Array.isArray(arr)) yield arr;
else for (let el of arr) yield* flatten(el);
}
// Example:
console.log(...flatten([1,[2,[3,[4]]]])); // 1 2 3 4Run Code Online (Sandbox Code Playgroud)
如果需要,创建一个展平值数组,如下所示:
let flattened = [...flatten([1,[2,[3,[4]]]])]; // [1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
我宁愿将整个数组按原样转换为字符串,但与其他答案不同,它会使用JSON.stringify而不使用toString()会产生不需要的结果的方法.
使用该JSON.stringify输出,剩下的就是删除所有括号,再次使用开始和结束括号包装结果,并提供结果JSON.parse,使字符串返回"生命".
var arr = ["abc",[[[6]]],["3,4"],"2"];
var s = "[" + JSON.stringify(arr).replace(/\[|]/g,'') +"]";
var flattened = JSON.parse(s);
console.log(flattened)Run Code Online (Sandbox Code Playgroud)
有 2 种方法可以让您的阵列变平
* 1-第一个你可以使用 Array.prototype.flat 方法
const veryDeep = [[1, [2, 2, [3, [4, [5, [6]]]]], 1]];
veryDeep.flat(Infinity)
// [1, 2, 2, 3, 4, 5, 6, 1]
/** If you don't know the depth of the array,
simply pass Infinity **\
Run Code Online (Sandbox Code Playgroud)
或者,如果您知道数组嵌套了多少,则可以输入参数
const twoLevelsDeep = [[1, [2, 2], 1]];
//depth = 1
twoLevelsDeep.flat();
[1, [2, 2], 1]
//depth = 2
twoLevelsDeep.flat(2);
// [1, 2, 2, 1]
Run Code Online (Sandbox Code Playgroud)
* 2-Second 方法你可以通过在 ES6 中使用扩展运算符来展平你的数组,并且 Concat 它将展平你的数组,不管它有多少嵌套
const multidimension = [, [, , ], , [, ]];
[].concat(...multidimension); // This will return: [, , , , , ,
]
const multidimension = [1, [2, 3, 4], 5, [6, 7]];
[].concat(...multidimension); // This will return: [1, 2, 3, 4, 5, 6, 7]
Run Code Online (Sandbox Code Playgroud)
这并不难,只需遍历数组并合并它们:
var result = [], input = [["$6"], ["$12"], ["$25"], ["$25"], ["$18"]];
for (var i = 0; i < input.length; ++i) {
result = result.concat(input[i]);
}
Run Code Online (Sandbox Code Playgroud)
看起来这看起来像是RECURSION的工作!
码:
var flatten = function(toFlatten) {
var isArray = Object.prototype.toString.call(toFlatten) === '[object Array]';
if (isArray && toFlatten.length > 0) {
var head = toFlatten[0];
var tail = toFlatten.slice(1);
return flatten(head).concat(flatten(tail));
} else {
return [].concat(toFlatten);
}
};
Run Code Online (Sandbox Code Playgroud)
用法:
flatten([1,[2,3],4,[[5,6],7]]);
// Result: [1, 2, 3, 4, 5, 6, 7]
Run Code Online (Sandbox Code Playgroud)
小智 5
我已经使用递归和闭包完成了它
function flatten(arr) {
var temp = [];
function recursiveFlatten(arr) {
for(var i = 0; i < arr.length; i++) {
if(Array.isArray(arr[i])) {
recursiveFlatten(arr[i]);
} else {
temp.push(arr[i]);
}
}
}
recursiveFlatten(arr);
return temp;
}
Run Code Online (Sandbox Code Playgroud)
前几天我和ES6 Generators一起玩,并写下了这个要点.其中包含...
function flatten(arrayOfArrays=[]){
function* flatgen() {
for( let item of arrayOfArrays ) {
if ( Array.isArray( item )) {
yield* flatten(item)
} else {
yield item
}
}
}
return [...flatgen()];
}
var flatArray = flatten([[1, [4]],[2],[3]]);
console.log(flatArray);
Run Code Online (Sandbox Code Playgroud)
基本上我正在创建一个循环在原始输入数组上的生成器,如果它找到一个数组,它会使用yield*运算符和递归来连续展平内部数组.如果该项不是数组,则只生成单个项.然后使用ES6 Spread运算符(又名splat运算符),我将生成器展平为一个新的数组实例.
我没有测试过它的性能,但我认为它是使用生成器和yield*运算符的一个很好的简单例子.
但同样,我只是在玩弄,所以我确信有更多高效的方法可以做到这一点.
只是没有lodash的最佳解决方案
let flatten = arr => [].concat.apply([], arr.map(item => Array.isArray(item) ? flatten(item) : item))
Run Code Online (Sandbox Code Playgroud)
// using Es6 flat()
let arr = [1,[2,[3,[4,[5,[6,7],8],9],10]]]
console.log(arr.flat(Infinity))
// using Es6 reduce()
let flatIt = (array) => array.reduce(
(x, y) => x.concat(Array.isArray(y) ? flatIt(y) : y), []
)
console.log(flatIt(arr))
// using recursion
function myFlat(array) {
let flat = [].concat(...array);
return flat.some(Array.isArray) ? myFlat(flat) : flat;
}
console.log(myFlat(arr));
// using string manipulation
let strArr = arr.toString().split(',');
for(let i=0;i<strArr.length;i++)
strArr[i]=parseInt(strArr[i]);
console.log(strArr)Run Code Online (Sandbox Code Playgroud)
我认为 array.flat(Infinity) 是一个完美的解决方案。但扁平函数是一个相对较新的函数,可能无法在旧版本的浏览器中运行。我们可以使用递归函数来解决这个问题。
const arr = ["A", ["B", [["B11", "B12", ["B131", "B132"]], "B2"]], "C", ["D", "E", "F", ["G", "H", "I"]]]
const flatArray = (arr) => {
const res = []
for (const item of arr) {
if (Array.isArray(item)) {
const subRes = flatArray(item)
res.push(...subRes)
} else {
res.push(item)
}
}
return res
}
console.log(flatArray(arr))Run Code Online (Sandbox Code Playgroud)
小智 5
这是递归方式...
function flatten(arr){
let newArray = [];
for(let i=0; i< arr.length; i++){
if(Array.isArray(arr[i])){
newArray = newArray.concat(flatten(arr[i]))
}else{
newArray.push(arr[i])
}
}
return newArray;
}
console.log(flatten([1, 2, 3, [4, 5] ])); // [1, 2, 3, 4, 5]
console.log(flatten([[[[1], [[[2]]], [[[[[[[3]]]]]]]]]])) // [1,2,3]
console.log(flatten([[1],[2],[3]])) // [1,2,3]
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用“join()”和“split()”:
let arrs = [
["$6"],
["$12"],
["$25"],
["$25"],
["$18"],
["$22"],
["$10"]
];
let newArr = arrs.join(",").split(",");
console.log(newArr); // ["$6", "$12", "$25", "$25", "$18", "$22", "$10"]Run Code Online (Sandbox Code Playgroud)
此外,您还可以使用“toString()”和“split()” :
let arrs = [
["$6"],
["$12"],
["$25"],
["$25"],
["$18"],
["$22"],
["$10"]
];
let newArr = arrs.toString().split(",");
console.log(newArr); // ["$6", "$12", "$25", "$25", "$18", "$22", "$10"]Run Code Online (Sandbox Code Playgroud)
但是,如果字符串包含逗号,上述两种方法都无法正常工作:
“join()”和“split()”:
let arrs = [
["$,6"],
["$,12"],
["$2,5"],
["$2,5"],
[",$18"],
["$22,"],
["$,1,0"]
];
let newArr = arrs.join(",").split(",");
console.log(newArr);
// ["$", "6", "$", "12", "$2", "5", "$2", "5", "", "$18", "$22", "", "$", "1", "0"]Run Code Online (Sandbox Code Playgroud)
“toString()”和“split()”:
let arrs = [
["$,6"],
["$,12"],
["$2,5"],
["$2,5"],
[",$18"],
["$22,"],
["$,1,0"]
];
let newArr = arrs.toString().split(",");
console.log(newArr);
// ["$", "6", "$", "12", "$2", "5", "$2", "5", "", "$18", "$22", "", "$", "1", "0"]Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
610350 次 |
| 最近记录: |