Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6640981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:45:26+00:00 2026-05-25T23:45:26+00:00

Using JavaScript, I’d like to split one big array of coordinates into smaller arrays

  • 0

Using JavaScript, I’d like to split one big array of coordinates into smaller arrays based on coinciding points. I am not 100% sure how to write the following in code but it describes what I’m attempting to achieve:

  1. Iterate through the array

    var A = [(1,2)(1,3)(2,3)(9,10)(9,11)(10,11)];

  2. Combine the pairs that contain any matching/identical coordinate points:

    var B = (1,2)(1,3)(2,3)

    var C = (9,10)(9,11)(10,11)

  3. Combine the matching/identical points and create new, smaller arrays from the combinations in point #2

    var D = [1,2,3]

    var E = [9,10,11]

Can I get help please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:45:27+00:00Added an answer on May 25, 2026 at 11:45 pm

    Working answer: http://jsfiddle.net/y3h9L/

    OK, so if I understand the requirement A is a one-dimensional array that is assumed to have an even number of elements in x,y pairs.

    A = [1,2,  1,3,  2,3,  9,10,  9,11,  10,11]
    // output should be
    [ [1,2,3], [9,10,11] ]
    
    // but if you add an extra pair that links the two halves, say add 2,11
    A2 = [1,2,  1,3,  2,3,  9,10,  9,11,  10,11,   2,11]
    // then all are related so output should be
    [ [1,2,3,9,10,11] ]
    

    I’ve made no effort to pretty-up or optimise the following code, but it works:

    // single dimensional array of x,y pairs
    var A = [1,2,  1,3,  2,3,  9,10,  9,11,  10,11];
    
    // create a working copy of A so that we can remove elements
    // and still keep the original A intact.
    var workingCopy = A.slice(0, A.length),
        matchedPairs = [],
        currentMatches,
        finalCombinations = [],
        x, y, i, j,
        tempArray;
    
    while (workingCopy.length > 0) {
       currentMatches = [];
       currentMatches.push([workingCopy.shift(),workingCopy.shift()]);
    
       workingCopyLoop:
       for (x=0,y=1; x < workingCopy.length;) {
          for (i=0; i < currentMatches.length; i++){
             if (workingCopy[x] === currentMatches[i][0]
                || workingCopy[y] === currentMatches[i][1]) {
                currentMatches.push([workingCopy.shift(),workingCopy.shift()]);
                // go back to the beginning of workingCopyLoop
                x=0;
                y=1;
                continue workingCopyLoop;
             }
          }
    
          x += 2;
          y += 2;
       }   
    
       matchedPairs.push(currentMatches);
    }
    
    for (i=0; i<matchedPairs.length; i++){
       tempArray = [];
       for (j=0; j<matchedPairs[i].length; j++) {
          // I assume you have a new enough version of JS that you have Array.indexOf()
          if (-1 === tempArray.indexOf(matchedPairs[i][j][0]))
             tempArray.push(matchedPairs[i][j][0]);
          if (-1 === tempArray.indexOf(matchedPairs[i][j][1]))
             tempArray.push(matchedPairs[i][j][1]);
       }
       finalCombinations.push(tempArray);
    }
    
    for (i=0; i<finalCombinations.length; i++)
       console.log(finalCombinations[i]);
    
    // console.log shows that finalCombinations = [ [1,2,3], [9,10,11] ]
    

    If it’s not obvious how this works, follow it through with a debugger and/or pencil and paper.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using JavaScript I'm trying to split a paragraph into it's sentences using regular expressions.
Using JavaScript, I have created an array of objects. One of the object's properties
Using javascript, I've drawn several polygon images on my canvas. I'd like to, based
Using javascript, How can I convert a human time string like Wed Jun 20
Using Javascript, I would like change the background color of the numbers in an
Using JavaScript I'd like to get the domain value for a specific cookie. Is
Using JavaScript how do you to detect what text the user pastes into a
I am reading a book about Javascript and jQuery and using one of the
Using javascript, I am trying to change the selection of the listbox item like
Using Javascript, I would like to round a number passed by a user to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.