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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:38:43+00:00 2026-06-04T07:38:43+00:00

I’m trying to make something similar to an array. I need to be able

  • 0

I’m trying to make something similar to an array.

I need to be able to “release” an Index (set its value to undefined) but I don’t want to lose the Index.
The “released” Indexes should be re-used whenever a new item is put into the array.

I’d like to be able to do something like:

example = new MyArray();

a = example.leaseIndex(); // returns 0
example[a] = "example value";

b = example.leaseIndex(); // returns 1
example[b] = "another value";

example.releaseIndex(0);

c = example.leaseIndex(); // returns 0
example[c] = "yet another value";

In my example leaseIndex finds an available index or if none are available pushes a new item onto the array and returns the index of that item.

I want to do this so that the array doesn’t just grow bigger over time needlessly.
I can’t remove the “released” items as each item in the array contains a reference to another item in the same array.

I’ve had minor successes with functions and arrays outside of the main one to keep track of the available indexes and assign and release them, but ideally I’d like the functionality to be part of the main array.

Would I have to add my functions to the Array (or its prototype) or is there another way? As not all my arrays need this functionality.

Hope this makes sense :/

update

I am trying to store wiring loom layouts, which are essentially a net diagram (points and information as to how the points are connected).

The picture shows an example loom. It has 3 Connectors; Red (0) with 2 Lines, Yellow (1) with 3 Lines and Green (2) with 2 Lines.
One of the lines on the Red Connector is Spliced (allowing multiple Lines to connect to a Single Line, the blue square)

enter image description here

This is how that loom would be stored.

loom = {
    points = [
        { self: 0, list: [ 2 ] },
        { self: 1, list: [ 7 ] },
        { self: 2, list: [ 0 ] },
        { self: 3, list: [ 7 ] },
        { self: 4, list: [ 6 ] },
        { self: 5, list: [ 7 ] },
        { self: 6, list: [ 4 ] },
        { self: 7, owner: 1, list: [ 1, 3, 5 ] }
    ],
    connectors = [
        [ 0, 1 ],
        [ 2, 3, 4 ],
        [ 5, 6 ]
    ]
}

the elements in the connectors array contain the indexes of points in the points array.
the list array inside each points object contains the index of its destination(s) which are points too.

Im trying to make functions to help make managing the indexes easier, just wanted to know if there is a way to extend the array, or make something similar that incorporates the functionality. Using static functions would be OK and is what I’ve been using. I just wanted to see if i could extend the array, or use something like one so I didnt need to use the static functions.

  • 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-04T07:38:44+00:00Added an answer on June 4, 2026 at 7:38 am

    I would add the methods to the Array’s prototype, like this:

    Array.prototype.leaseIndex = function () {
       for (var i = 0; i < this.length; i++) {
         if(typeof this[i] === "undefined") {
           return i;
         }
       }
       return this.length;
    };
    Array.prototype.releaseIndex = function (index) {
      delete this[index];
    };
    

    So, your code would look like this:

    example = [];
    
    a = example.leaseIndex(); // returns 0
    example[a] = "example value";
    
    b = example.leaseIndex(); // returns 1
    example[b] = "another value";
    
    example.releaseIndex(0);
    
    c = example.leaseIndex(); // returns 0
    example[c] = "yet another value";
    

    I hope it helps.

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

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.