Using .Map() on nested arrays

Hello!

Thanks for taking the time to read this.

I’m looking to better understand how to use map on nested arrays.

The following code accesses a nested array and adds one to every single number.

let arr = [[1,2],[3,4],[5,6]];

let arr2 = arr.map(ele=> ele.map((ele) =>ele + 1))

What I want to do, and haven’t been able to figure out is this.

  • How can I add one to every n’th number in each array

  • How can I filter out non integers while using map? Consider the following.

let arr = [[A,2],[B,4],[C,6]];

let arr2 = arr.map(ele=> ele.filter(?????))

How do I use the filter function to return just an array of numbers? I’ve been messing around in a JavaScript testbed and I can’t seem to figure it out. I’ve read the Mozilla documentation but it still eludes me.

Thanks for the help!

Each element in that array is an array of two numbers. You don’t need to map each sub-array, just return an array where the first element is the same and the second one is the same + 1.

If you want to filter it, again, you know each element is an array of a string then a number (I assume that’s what you meant). So just return the second element of that sub-array.

Its slightly unclear what you’re trying to do, but if you have an array of arrays, and those sub-arrays are a mixture of numbers and other things and the sub-arrays are of unknown length, and you want to produce a single array of numbers, then flatMap is what you want (maps and flattens), eg

const arrayOfArrays = [
  [1, 2, "foo"],
  [3, true, "bar"],
  [false, "baz", 4],
];

const isNumber = (value) => typeof value === "number";

const nums = arrayOfArrays.flatMap((subArr) => subArr.filter(isNumber));

console.log(nums)
// [1, 2, 3, 4]
3 Likes

Hi Dan,

I figured it out after a little more digging and from your advice.

What I meant was something like this.

let arr = [[10, 20],[30,40]]

let p = [];
p = arr.map(ele => ele.map((ele, index) => index == 1   ? ele + 1: ele))

This will return

 [[10, 21],[30,41]]

So this will let me manipulate the second value in an array of nested arrays. For some reason I neglected the index argument when reading the .map() documentation, Thanks for pointing me in the correct direction.

I didn’t want to flatten the array because I’d prefer to manipulate it in place to keep count of where the values I want are.

You know that each sub array has two elements. You don’t need to map the sub array, you just return the exact values you want. So as an example for a single array:

function addOneToTheSecondElement ([a, b]) {
  return [a, b + 1];
}
1 Like
let n = 5 // the index of the element your want to add 1 to
let arr2 = arr.map(elem=> elem[n]+=1)
1 Like

So this

let arr2 = arr.map(elem=> elem[n]+=1)

Is going to do exactly that as well as mutating the arrays.

With that, you’re saying “for every elem in art, mutate the nth element by assigning its value + 1 to it.” That action will also return the new value, so for example:

const arr = [[1,2],[3,4]]
const newArr = arr.map(elem => elem[1] += 1)
console.log(arr) // [[1,3],[3,5]]
console.log(newArr) // [3,5]

You want to return a new array each time

return [elem[0], elem[1] + 1];
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.