如何提取多维数组中每个子数组的第一个元素?

时间:2019-05-13 13:11:56

标签: javascript multidimensional-array google-apps-script

我有一个多维数组:

var array 1 = 

[

 [[Name 1, 2, Nigeria], 
  [Name 3, 52, Egypt], 
  [Name 5, 75, South Africa]]

 [[Name 5, 8, Nigeria], 
  [Name 1, 62, Egypt], 
  [Name 3, 115, South Africa]]

 [[Name 6, 88, Nigeria], 
  [Name 3, 92, Egypt], 
  [Name 5, 825, South Africa]]

 ]

我想要一个新的平面阵列:

var array 2 = [Name 1, Name 3, Name 5, Name 5, Name 1, Name 3, Name 6, Name 3, Name 5]

我尝试编写一个映射到数组并返回第一个元素的函数:

function name(filteredName){
filteredName.map(function(firstName){
  return firstName[0]
}) 

}

但是,这只会返回:

[Name 1, Name 1, Name 1] 

我真的不确定如何解决这个问题!任何帮助都会很棒。

6 个答案:

答案 0 :(得分:2)

您可以先使用嵌套的map(),然后再使用flat()

var arr = [ [['Name 1', 2, 'Nigeria'], ['Name 3', 52, 'Egypt'], ['Name 5', 75, 'South Africa']], [['Name 5', 8, 'Nigeria'], ['Name 1', 62, 'Egypt'], ['Name 3', 115, 'South Africa']], [['Name 6', 88, 'Nigeria'], ['Name 3', 92, 'Egypt'], ['Name 5', 825, 'South Africa']] ];
 
   const res = arr.map(x => x.map(a => a[0])).flat(2)
   console.log(res)

没有flat()

您可以不使用flat()和散布运算符来使用concat()

var arr = [ [['Name 1', 2, 'Nigeria'], ['Name 3', 52, 'Egypt'], ['Name 5', 75, 'South Africa']], [['Name 5', 8, 'Nigeria'], ['Name 1', 62, 'Egypt'], ['Name 3', 115, 'South Africa']], [['Name 6', 88, 'Nigeria'], ['Name 3', 92, 'Egypt'], ['Name 5', 825, 'South Africa']] ];
 
 const res = [].concat(...arr.map(x => x.map(x => x[0])))
 console.log(res)

答案 1 :(得分:1)

我遍历第一个数组。在该循环中,我遍历第二个并推入第一个条目。

var array = [

 [['Name 1', 2, 'Nigeria'], 
  ['Name 3', 52, 'Egypt'], 
  ['Name 5', 75, 'South Africa']],

 [['Name 5', 8, 'Nigeria'], 
  ['Name 1', 62, 'Egypt'], 
  ['Name 3', 115, 'South Africa']],

 [['Name 6', 88, 'Nigeria'], 
  ['Name 3', 92, 'Egypt'], 
  ['Name 5', 825, 'South Africa']],
 ];
 
var result = []

for (var i = 0; i < array.length; i++) {
  for (var j = 0; j < array[i].length; j++) {
    result.push(array[i][j][0])
  }
}

console.log(result)

答案 2 :(得分:1)

您可以像这样使用mapflatMap的组合:

const array1 = [ [['Name 1', 2, 'Nigeria'], ['Name 3', 52, 'Egypt'], ['Name 5', 75, 'South Africa']], [['Name 5', 8, 'Nigeria'], ['Name 1', 62, 'Egypt'], ['Name 3', 115, 'South Africa']], [['Name 6', 88, 'Nigeria'], ['Name 3', 92, 'Egypt'], ['Name 5', 825, 'South Africa']] ];

const output = array1.flatMap(a => a.map(b => b[0]))

console.log(output)

如果不支持flatMap,则可以使用简单的嵌套for...of循环:

var array1 = [ [['Name 1', 2, 'Nigeria'], ['Name 3', 52, 'Egypt'], ['Name 5', 75, 'South Africa']], [['Name 5', 8, 'Nigeria'], ['Name 1', 62, 'Egypt'], ['Name 3', 115, 'South Africa']], [['Name 6', 88, 'Nigeria'], ['Name 3', 92, 'Egypt'], ['Name 5', 825, 'South Africa']] ];

var output = [];

for (var arr of array1) {
  for (var arr2 of arr) {
    output.push(arr2[0])
  }
}

console.log(output)

答案 3 :(得分:1)

您可以像这样使用Array.flat()Array.map()

array1.flat().map(arr => arr[0]);

或者您可以使用Array.concat()代替Array.flat():

[].concat(...array1).map(arr => arr[0]);

工作示例

var array1 = [
 [
   ['Name 1', 2, 'Nigeria'], 
   ['Name 3', 52, 'Egypt'], 
   ['Name 5', 75, 'South Africa']
 ],

 [
   ['Name 5', 8, 'Nigeria'], 
   ['Name 1', 62, 'Egypt'], 
   ['Name 3', 115, 'South Africa']
 ],

 [
   ['Name 6', 88, 'Nigeria'], 
   ['Name 3', 92, 'Egypt'], 
   ['Name 5', 825, 'South Africa']
 ]
];

const NamesArr = array1.flat().map(arr => arr[0]);

console.log(NamesArr);
console.log('Array.concat():');
console.log([].concat(...array1).map(arr => arr[0]));

答案 4 :(得分:0)

在使用.map之前将数组放平。这样,您将不再使用多维数组,并且更容易获得每个数组的第一个元素:

var arr = 
[
 [['Name 1', 2, 'Nigeria'], 
  ['Name 3', 52, 'Egypt'], 
  ['Name 5', 75, 'South Africa']],

 [['Name 5', 8, 'Nigeria'], 
  ['Name 1', 62, 'Egypt'], 
  ['Name 3', 115, 'South Africa']],

 [['Name 6', 88, 'Nigeria'], 
  ['Name 3', 92, 'Egypt'], 
  ['Name 5', 825, 'South Africa']]

 ]
 
 
 console.log(arr.flat().map(item => item[0]))

通知

.flat还没有强大的支持,如果要使用最新的代码标准,则可以使用polyfill,也可以使用传播运算符来展平数组:

var arr = 
[
 [['Name 1', 2, 'Nigeria'], 
  ['Name 3', 52, 'Egypt'], 
  ['Name 5', 75, 'South Africa']],

 [['Name 5', 8, 'Nigeria'], 
  ['Name 1', 62, 'Egypt'], 
  ['Name 3', 115, 'South Africa']],

 [['Name 6', 88, 'Nigeria'], 
  ['Name 3', 92, 'Egypt'], 
  ['Name 5', 825, 'South Africa']]

 ]
 
 
 console.log([].concat(...arr).map(item => item[0]))

答案 5 :(得分:0)

您还可以使用forEach和传播语法

var flatArr = [];

array_1.forEach((arr, i) => {
    arr.forEach(innerArr => {
        flatArr = [...flatArr, innerArr[0]]
    })
});