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

211
Visualizações
Foreach Array Element Search for values in multidimension Array javascript

Trying to match each element of an array to a set of coordinates in a multidimensions array in the following manner:

array1= [0, 5, 4]
array2 = [
{x: 1, y: 4, name: 'A', w: 0}, 
{x: 2, y: 8, name: 'E', w: 4}, 
{x: 3, y: 1, name: 'F', w: 5}];

I am hoping to match each element of array 1 to the value of w in array 2

0 -> {x: 1, y: 4, name: 'A', w: 0}
5 ->  {x: 3, y: 1, name: 'F', w: 5}
4 -> {x: 2, y: 8, name: 'E', w: 4}

I want to return :

[
{x:1, y,4}, {x:3, y:1},
{x:3, y:1}, {x:2, y:8},
...
];
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can chain array filter and map to do something like this

Array.prototype.filter() to filter all elements based on the index from first array and

Array.prototype.map() to modify the object so as to only show the x and y co-ordinates

const array1 = [0, 5, 4];
const array2 = [
  { x: 1, y: 4, name: "A", index: 0 },
  { x: 2, y: 8, name: "E", index: 4 },
  { x: 3, y: 1, name: "F", index: 5 },
];

const newArr = array2.filter(x => array1.includes(x.index)).map(x => ({ x: x.x, y: x.y }));
console.log(newArr)

about 4 years ago · Juan Pablo Isaza Relatório

0

You should return the required x and y coordinates as an array.

Please find a working fiddle for that.

const array1 = [0, 5, 4]
const array2 = [
  { x: 1, y: 4, name: 'A', w: 0 },
  { x: 2, y: 8, name: 'E', w: 4 },
  { x: 3, y: 1, name: 'F', w: 5 },
];
const tempArray = array1.map((item) => array2.find((node) => node.w === item));
// console.log(tempArray);
const finalArray = tempArray.map((currentNode, index, actualArray) => {
  const nextIndex = index === actualArray.length - 1 ? 0 : index + 1;
  return [
    { x: currentNode.x, y: currentNode.y }, 
    { x: actualArray[nextIndex].x, y: actualArray[nextIndex].y }, 
  ];
});
console.log(finalArray);

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