Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
javascript array filter returning same array

I have an array of neighbors to a node that I'm trying to filter based on the node property isVisited. Currently its returning the same array and I want it to only return an array that is !isVisited.

export function getUnvisitedNeighbors(grid, node) {
    const { row, col } = node;
    const neighbors = [];
    if (row < grid.length - 1) neighbors.push(grid[row + 1][col]);
    if (col < grid[0].length - 1) neighbors.push(grid[row][col + 1]);
    if (row > 0) neighbors.push(grid[row - 1][col]);
    if (col > 0) neighbors.push(grid[row][col - 1]);
    console.log("before");
    console.log(neighbors);
    neighbors.filter(neighbor => !neighbor.isVisited);     //returning same array
    console.log("after")
    console.log(neighbors);
    return neighbors;
}

console log: enter image description here

how I created the nodes:


function createNode(row, col) {
    return {
        isVisited: false,
        row: row,
        col: col,
        startnode: row === START_NODE_ROW && col === START_NODE_COL,
        endnode: row === END_NODE_ROW && col === END_NODE_COL,
        distance: Infinity,
        isWall: false,
        previousNode: null
    }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

assign the result to your variable

neighbors = neighbors.filter(neighbor => !neighbor.isVisited);
about 4 years ago · Juan Pablo Isaza Relatório

0

I see that several people have told you the immediate problem. You might also consider changing the logic so that you simply map from the grid array where distance from the node is 1 and isVisited. The only way for the distance to be 1 is if they are above, below, right or left 1 increment (diagonal would be sqrt(2)). You could write it in one line, although I would probably write a distance formula. You can eliminate a few lines and also avoid the checks to see if your numbers are within bounds.

const node = {row: 4, col: 10};
//const {row, col} = node;

const grid = [
  {isVisited: true,  row: 3, col: 9 },
  {isVisited: false, row: 3, col: 10}, 
  {isVisited: true,  row: 4, col: 9 },
  {isVisited: false, row: 4, col: 11},
  {isVisited: true,  row: 5, col: 10},
  {isVisited: true,  row: 5, col: 11}
];

const dist=(n,r,c)=>{return Math.abs(Math.sqrt(Math.pow(n.row-r, 2)+Math.pow(n.col-c, 2)))}

let neighbors = grid.filter(
  function(e) {return e.isVisited && dist(e, this.row, this.col) == 1;
}, node);

console.log(neighbors);

The distance formula up there looks a little clunky, but it's just the same distance formula from high school geometry:

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda