Skip to content Skip to sidebar Skip to footer

How To Remove Last Element From Every Row Of A Matrix In Javascript

I am trying to remove the last element from every row of a matrix in javascript. I am trying to use the 'map' function but I am not successful. Here is my code: I get TypeError

Solution 1:

The first argument to .map is the item you're iterating over, not the index.

Since each item here is an array, you can either .pop the array (which will mutate the existing array), or .slice the array (which will not mutate the existing array).

var matrixWithExtraInfo = [
    [1,2,3,4,"dog"],
    [5,6,7,8,"dog"],
    [9,10,11,12,"dog"],
    [13,14,15,16,"dog"],
    [17,18,19,20,"dog"]
];

var conciseMatrix = [
    [1,2,3,4],
    [5,6,7,8],
    [9,10,11,12],
    [13,14,15,16],
    [17,18,19,20]
]

var conciseMatrix = matrixWithExtraInfo.map((arr) => {
  arr.pop();
  return arr;
});
console.log(matrixWithExtraInfo);
console.log(conciseMatrix);

(the above is weird - the structure you need is already in matrixWithExtraInfo, making another variable to hold it is confusing, but this is the closest to your original code)

var matrixWithExtraInfo = [
    [1,2,3,4,"dog"],
    [5,6,7,8,"dog"],
    [9,10,11,12,"dog"],
    [13,14,15,16,"dog"],
    [17,18,19,20,"dog"]
];

var conciseMatrix = [
    [1,2,3,4],
    [5,6,7,8],
    [9,10,11,12],
    [13,14,15,16],
    [17,18,19,20]
]

var conciseMatrix = matrixWithExtraInfo.map(arr => arr.slice(0, -1));
console.log(matrixWithExtraInfo);
console.log(conciseMatrix);

Solution 2:

If you want to mutate the existing array, an easy way is to use Array.forEach() to apply Array.pop() to each element:

var matrixWithExtraInfo = [
  [1, 2, 3, 4, "dog"],
  [5, 6, 7, 8, "dog"],
  [9, 10, 11, 12, "dog"],
  [13, 14, 15, 16, "dog"],
  [17, 18, 19, 20, "dog"]
];

matrixWithExtraInfo.forEach(arr => arr.pop());
console.log(matrixWithExtraInfo);

Otherwise, just use Array.slice():

var matrixWithExtraInfo = [
  [1, 2, 3, 4, "dog"],
  [5, 6, 7, 8, "dog"],
  [9, 10, 11, 12, "dog"],
  [13, 14, 15, 16, "dog"],
  [17, 18, 19, 20, "dog"]
];

var conciseMatrix = matrixWithExtraInfo.map(arr => arr.slice(0, -1));
console.log(conciseMatrix);

Solution 3:

Map will return the element so by iterating all rows we just pop the element which will remove the last element from the each row. As we need a new matrix in different array we should use slice(not pop/splice) which will return the elements in new array and not modifying original matrix.

var matrixWithExtraInfo = [
    [1,2,3,4,"dog"],
    [5,6,7,8,"dog"],
    [9,10,11,12,"dog"],
    [13,14,15,16,"dog"],
    [17,18,19,20,"dog"]
];

var conciseMatrix = [
    [1,2,3,4],
    [5,6,7,8],
    [9,10,11,12],
    [13,14,15,16],
    [17,18,19,20]
]

var conciseMatrix = matrixWithExtraInfo.map(row => row.slice(0, row.length-1));
console.log(matrixWithExtraInfo);
console.log(conciseMatrix);

Solution 4:

Just adding _, in map function in your code should fix. function(_,index)

var matrixWithExtraInfo = [
  [1, 2, 3, 4, "dog"],
  [5, 6, 7, 8, "dog"],
  [9, 10, 11, 12, "dog"],
  [13, 14, 15, 16, "dog"],
  [17, 18, 19, 20, "dog"]
];

var conciseMatrix = [
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9, 10, 11, 12],
  [13, 14, 15, 16],
  [17, 18, 19, 20]
]

var conciseMatrix = matrixWithExtraInfo.map(function(_,index) {
  console.log(index)
  matrixWithExtraInfo[index].pop();
  return matrixWithExtraInfo[index];
});
console.log(matrixWithExtraInfo);

Post a Comment for "How To Remove Last Element From Every Row Of A Matrix In Javascript"