8

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.

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? Commented May 13, 2019 at 13:18
  • No that is the full element. Would be easier if it was key/values. Commented May 13, 2019 at 13:18
  • so like Name = 1 and Nigeria = 2 or Name = [1, 2] and Nigeria = undefined? 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. Commented May 13, 2019 at 13:21

6 Answers 6

10

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Although Google App Script uses ES5, so no arrow functions.
5

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

Comments

3

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)

Comments

1

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)

Comments

0

you could also do it with forEach and spread syntax

var flatArr = [];

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

Comments

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

3 Comments

For some reasons, I have "TypeError: arr.flat is not a function" and this for all answer using methods such as flat(), map(), flatMap
using firefox 60.6.3
You guys are 100% right, .flat has been introduced in firefox 62. Anyway, gave you an alternative

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.