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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:11:18+00:00 2026-05-24T09:11:18+00:00

I want to create a helper object that will work the following way: It

  • 0

I want to create a helper object that will work the following way:
It is an array with a given number of rows, for example let’s say it can hold only 3 rows.
It will have an insert method that inserts the a new entry at the first index, and pushes one index down all the rest (new entry is 0, 0 becomes 1, 1–>2 etc..)
and if the array is full to the max number of rows, the last entry drops out.

So I made it the following way:

function limArray(array, maxlength){

    this.arr = array;
    this.maxlength = maxlength;
    this.arr.length = maxlength;

    this.insertVal = function(value){ //insert new value and push down the rest

        for (var i=maxlength; i>=0; i--) { 
            this.arr[i] = this.arr[i-1]
        };
        this.arr[0] = value;
        this.arr.length = maxlength;
    };
}

My question is, if it’s a smart way to make this?
Is it possible to create instance of the Array object itself and modify it to be limited etc..
Any critique / improvement suggestions are welcome!

  • 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-24T09:11:19+00:00Added an answer on May 24, 2026 at 9:11 am

    You could overwrite the push method:

    function limArray(array, maxlength){
        var push_ = array.push,
            slice_ = [].slice;
    
        array.push = function() {
            var values = slice_.call(arguments, 0, maxlength - this.length);
            return push_.apply(this, values);
        }
        return array;
    }
    

    As @alnorth29 mentioned, you can use unshift to prepend a value. You can overwrite this as well:

    function limArray(array, maxlength){
        // push like above
        var unshift_ = array.unshift;
        array.unshift = function() {
            unshift_.apply(this, arguments);
            this.length = this.length > maxlength ? maxlength : this.length;
            return this.length;
        }
    
        return array;
    }
    

    Of course you can also attach a new property with a more expressive name than unshift:

    array.prepend = array.unshift;
    

    If you extend an array instance like this, make sure you only traverse over its elements with a for loop, not with a for...in loop (you should always use for for arrays anyway). Another possibility is to define the property using ECMAScript 5’s Object.defineProperty [docs] and set enumerable to false. But it is only supported in newer browsers.

    There are other ways to add elements to an array, but I assume you are not going to use them .

    Usage:

    > var limited = limArray([], 3);
    > limited.push(1,2,3,4);
      3
    > limited
      [1, 2, 3]
    > limited.push(5)
      3
    > limited
      [1, 2, 3]
    
    > var limited = limArray([], 3);
    > limited.unshift(3, 4)
      2
    > limited
      [3, 4]
    > limited.unshift(1, 2)
      3
    > limited
      [1, 2, 3]
    

    Update:

    I forgot, in case the array passed to the function already exceeds the maximum number of elements, you have to reduce the length:

    function limArray(array, maxlength){
        array.length = array.length > maxlength ? maxlength : array.length;
        //...
    
        return array;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a hidden field and create a link in one helper
I am using the Html.TextBox helper to create textboxes. I want to set attributes
I want to create a function that performs a function passed by parameter on
I want to create a simple http proxy server that does some very basic
I want to create an allocator which provides memory with the following attributes: cannot
Trying to construct a helper class that will return an arraylist, but I'm getting
I want to create a listBox: http://blog.wekeroad.com/blog/aspnet-mvc-preview-using-the-mvc-ui-helpers/ The Html.ListBox doesn't work: <div class=editor-field> <%
I want create a drop shadow around the canvas component in flex. Technically speaking
I want to create a Java application bundle for Mac without using Mac. According
I want to create a client side mail creator web page. I know the

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.