Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!