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 9189393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:13:36+00:00 2026-06-17T20:13:36+00:00

I have converted a JavaScript spritesheet to a 2D integer array, and now I’m

  • 0

I have converted a JavaScript spritesheet to a 2D integer array, and now I’m trying to split a 2D array of integers into multiple 2D arrays, using 1 as the “separator” number.

Spritesheet
Is there any way to separate a 2D JavaScript array like the following into multiple arrays using a separator number, as shown below?

function separate2DArray(arrToSeparate, separator){
    //separate the 2D array into multiple 2D arrays, using a
    //specific number as the separator
}

//array to separate:
[
[5, 5, 5, 1, 5, 4, 5],
[5, 5, 4, 1, 4, 3, 4],
[1, 1, 1, 1, 1, 1, 1], //1 is the "separator number", which splits the array
[9, 2, 1, 4, 2, 4, 5],       //horizontally and vertically
]
//The array above would produce the following 2D arrays:

5 5 5
5 5 4

5 4 5
4 3 4

9 2

4 2 4 5

The main application for this algorithm that I have in mind is spritesheet image separation.

  • 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-06-17T20:13:37+00:00Added an answer on June 17, 2026 at 8:13 pm

    Given that the separated areas are rectangular, this will work:

    function separate2DArray(array, sep){
        //separate the 2D array into multiple 2D arrays, using a
        //specific number as the separator
        var result = [],
            currentSubs = {}; // using x coordinate as key
    
        for (var y=0; y<array.length; y++) {
            var line = array[y],
                subBegin = 0;
            for (var x=0; x<=line.length; x++) {
                if (x == line.length || line[x] == sep) {
                    if (subBegin < x) {
                        var sub = line.slice(subBegin, x);
                        if (subBegin in currentSubs)
                            currentSubs[subBegin].push(sub);
                        else
                            currentSubs[subBegin] = [sub];
                    } else { // a line of separators, subBegin == x
                        if (subBegin in currentSubs) {
                            result.push(currentSubs[subBegin]);
                            delete currentSubs[subBegin];
                        }
                    }
                    subBegin = x+1;
                }
            }
        }
        for (var begin in currentSubs)
            result.push(currentSubs[begin]);
        return result;
    }
    

    The result here is just a very simple array of the subareas, without any information about their position in the original area. Improved version:

    function separate2DArray(array, sep){
        var result = [],
            currentSubs = {};
        for (var y=0; y<array.length; y++) {
            var line = array[y],
                subBegin = 0;
            for (var x=0; x<=line.length; x++) {
                if (x == line.length || line[x] == sep) {
                    if (subBegin < x) {
                        var subline = line.slice(subBegin, x);
                        if (! (subBegin in currentSubs)) {
                            var subarea = [];
                            result.push({x:x, y:y, area:subarea});
                            currentSubs[subBegin] = subarea;
                        }
                        currentSubs[subBegin].push(subline);
                    } else {
                        if (subBegin in currentSubs)
                            delete currentSubs[subBegin];
                    }
                    subBegin = x+1;
                }
            }
        }
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a Javascript project I have an json string converted into a Javascript object.
I have recently converted 10 JavaScript files into one file, which I then run
I have been playing with javascript arrays and I have run into, what I
How can I convert a javascript array to a php array?? I have a
I have converted an array to object data like this: <?php $myobject->data = (object)Array('zero','one','two');
I have a GPX Track (Startpoint == Endpoint) converted to GeoJSON. I now want
I have successfully converted the FullCalendar into asp.net / VB and it works perfectly.
I have an array that I want converted to a comma delimited string. Array.toString()
I have a JavaScript array that contains 150 JavaScript functions (these functions return financial
I have a small piece of code that needs to split a string into

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.