我有一个JavaScript数组,如:

[["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]]

如何将单独的内部数组合并为一个,例如:

["$6", "$12", "$25", ...]

当前回答

您可以使用“join()”和“split()”:

设arrs=[["$6"],["$12"],["$25"],["$25"],["$18"],["$22"],["$10"]];让newArr=arrs.join(“,”).split(“,“);console.log(newArr);//["$6", "$12", "$25", "$25", "$18", "$22", "$10"]

此外,还可以使用“toString()”和“split()”:

设arrs=[["$6"],["$12"],["$25"],["$25"],["$18"],["$22"],["$10"]];让newArr=arrs.toString().split(“,”);console.log(newArr);//["$6", "$12", "$25", "$25", "$18", "$22", "$10"]

然而,如果字符串包含逗号,上述两种方式都不能正常工作:

“join()”和“split()”:

设arrs=[["$,6"],["$,12"],["$2,5"],["$2,5"],[",$18"],["$22,"],["$,1,0"]];让newArr=arrs.join(“,”).split(“,“);console.log(newArr);// ["$", "6", "$", "12", "$2", "5", "$2", "5", "", "$18", "$22", "", "$", "1", "0"]

“toString()”和“split()”:

设arrs=[["$,6"],["$,12"],["$2,5"],["$2,5"],[",$18"],["$22,"],["$,1,0"]];让newArr=arrs.toString().split(“,”);console.log(newArr);// ["$", "6", "$", "12", "$2", "5", "$2", "5", "", "$18", "$22", "", "$", "1", "0"]

其他回答

如何使用JavaScript 1.8的reduce(callback[,initialValue])方法

list.reduce((p,n) => p.concat(n),[]);

能胜任这项工作。

ES6单线压扁

参见lodash flatten,下划线flatten(浅真)

function flatten(arr) {
  return arr.reduce((acc, e) => acc.concat(e), []);
}

or

function flatten(arr) {
  return [].concat.apply([], arr);
}

使用测试

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]);
});

ES6单线深压平

请参见lodash flattendep,下划线flatten

function flattenDeep(arr) {
  return arr.reduce((acc, e) => Array.isArray(e) ? acc.concat(flattenDeep(e)) : acc.concat(e), []);
}

使用测试

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]);
});

我认为array.flat(Infinity)是一个完美的解决方案。但平面功能是一个相对较新的功能,可能不会在旧版本的浏览器中运行。我们可以使用递归函数来解决这个问题。

常量arr=[“A”,[“B”,[[“B11”,“B12”,[”B131“,“B132”]],“B2”]],”C“,[”D“,”E“,”F“,[“G”,“H”,“I”]]]常量flatArray=(arr)=>{常量res=[]for(arr的常量项){if(Array.isArray(项)){const subRes=flatArray(项)资源推送(…subRes)}其他{res.push(项目)}}返回res}console.log(flatArray(arr))

适用于所有数据类型的递归版本

 /*jshint esversion: 6 */

// nested array for testing
let nestedArray = ["firstlevel", 32, "alsofirst", ["secondlevel", 456,"thirdlevel", ["theinnerinner", 345, {firstName: "Donald", lastName: "Duck"}, "lastinner"]]];

// wrapper function to protect inner variable tempArray from global scope;
function flattenArray(arr) {

  let tempArray = [];

  function flatten(arr) {
    arr.forEach(function(element) {
      Array.isArray(element) ? flatten(element) : tempArray.push(element);     // ternary check that calls flatten() again if element is an array, hereby making flatten() recursive.
    });
  }

  // calling the inner flatten function, and then returning the temporary array
  flatten(arr);
  return tempArray;
}

// example usage:
let flatArray = flattenArray(nestedArray);

使用排列运算符:

常量输入=[[“$6”],[“$12”],【“$25”】,[“$25“],【”$18“】,【”$22“】,[”$10“]];常量输出=[].contat(…输入);console.log(输出);//-->["$6", "$12", "$25", "$25", "$18", "$22", "$10"]