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

  • Home
  • SEARCH
  • 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 8129389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:17:35+00:00 2026-06-06T08:17:35+00:00

I have created an utility function which I include in almost all Javascript code

  • 0

I have created an utility function which I include in almost all Javascript code I write. The purpose of the said function is to simply remove one element from an array. It takes an array and an index as the arguments, and returns the array which has the required element removed.

Here’s the function:

sliceHere = function(array, i)
{
    buffOne = array.slice(0, i);
    buffTwo = array.slice(i + 1);

    return buffOne.concat(buffTwo);         
}

It works quite alright. If I had an array a = ["a", "b", "c"], and I wanted to remove the "a" from it, I’d simply do a = sliceHere(a, 0);.

Although this did work, I wanted to replace it with something more “elegant”. What I wanted to do is to make the function sliceHere a method of all the arrays I have. So I could simply do a.sliceHere(0) and achieve the same effect. It just feels like a better way of doing things, correct me if I’m wrong.

What I did was this:

sliceHere = function(i)
{
    buffOne = this.slice(0, i);
    buffTwo = this.slice(i + 1);

    this = buffOne.concat(buffTwo);         
}

Array.prototype.sliceHere = sliceHere

This didn’t seem like it would work at all. Because I knew that “this” probably wasn’t the actual array. How do I reference the array from inside of a method?

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

    Why do you want to re-invent the wheel? Array.prototype.splice already does the same thing:

    var array = [1, 2, 3];
    var i = 1;
    array.splice(i, 1);
    array; // [2, 3]
    

    Okay, let’s re-invent the wheel:

    With splice:

    Array.prototype.sliceHere = function(i) {
        this.splice(i, 1);
    };
    

    Without splice:

    Array.prototype.sliceHere = function(i) {
         var i_want_you = this.slice(0, i).concat(this.slice(i + 1));
         this.length = 0;
         this.push.apply(this, i_want_you);  // <-- Modifies all keys, indirectly
    };
    

    If you do not want the method to show up in for(.. in ..) loops, use Object.defineProperty(Array.prototype, 'sliceHere', {value: /*function here*/});.

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

Sidebar

Related Questions

I have some Javascript code that needs to be dynamically created as a String
I've created a small utility function for string conversion so that I don't have
Ok, I have created a little utility with AppleScript and used Automator to turn
I have a smallish utility library I made that I had created in TFS
i have created a simple public ref class in the vc++ project, which is
For collection of smaller helper utility classes, I have created a general class MyUtils
I'd like to have a utility function that conditionally updates my request and response
I have a utility function in my Django project, it takes a queryset, gets
I have the following class which stores pairs of dates and prices: #include <vector>
I have created a few utility functions like one h() that acts as echo

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.