最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to extract first element of each child array in multidimensional array? - Stack Overflow

programmeradmin3浏览0评论

I have a multidimensional array:

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]]

 ]

I want to have a new flat array:

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

I've tried writing a function that maps over the array and returns the first element:

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

}

However, this just returns:

[Name 1, Name 1, Name 1] 

I'm really not sure how to solve this! Any help would be great.

I have a multidimensional array:

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]]

 ]

I want to have a new flat array:

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

I've tried writing a function that maps over the array and returns the first element:

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

}

However, this just returns:

[Name 1, Name 1, Name 1] 

I'm really not sure how to solve this! Any help would be great.

Share Improve this question edited May 13, 2019 at 13:13 Banzay 9,4702 gold badges30 silver badges46 bronze badges asked May 13, 2019 at 13:11 Ajay UbhiAjay Ubhi 3993 gold badges6 silver badges15 bronze badges 4
  • is name supposed to be a key and 1 supposed to be the value of name? also what is Name and Country? are those types or strings? – Michael Cacciano Commented May 13, 2019 at 13:18
  • No that is the full element. Would be easier if it was key/values. – Ajay Ubhi Commented May 13, 2019 at 13:18
  • so like Name = 1 and Nigeria = 2 or Name = [1, 2] and Nigeria = undefined? – Michael Cacciano Commented May 13, 2019 at 13:19
  • Provide minimal reproducible example. How are you calling name()? If you called name(array1), It won't return anything. Your array isn't a array either. It's syntactically inaccurate. Your example is neither complete nor verifiable. – TheMaster Commented May 13, 2019 at 13:21
Add a comment  | 

6 Answers 6

Reset to default 9

You can use nested map() and then 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)

Without flat()

You can do that without using flat() using concat() and spread operator.

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)

You can use Array.flat() and Array.map() like this:

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

Or you can use Array.concat() instead of Array.flat():

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

Working example:

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

You could use a combination of map and flatMap like this:

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)

If flatMap is not supported, you could use a simple nested for...of loop:

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)

I loop over the first array. In that loop, I loop over the second one and push the first entry.

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)

you could also do it with forEach and spread syntax

var flatArr = [];

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

Flatten your array before using .map. That way, you will not be working with a multidimensional array and it will be easier to get the first element of each array:

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

Notice

.flat does not have huge support yet, you can either use a polyfill if you want to use the latest code standard or use the spread operator to flatten your array:

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

发布评论

评论列表(0)

  1. 暂无评论