Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda