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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:02:45+00:00 2026-06-06T15:02:45+00:00

The problem is strange: I removed from Array (association) a few elements by splice

  • 0

The problem is strange: I removed from Array (association) a few elements by splice function, after this I added a few elements and suddenly there were “underfined” elements. Below there is the log (after ‘//’ this my comment to this):

// adding first element; array length = 1
["asdasdasd"] microblog.js:48

// adding second element; array length = 2
["asdasdasd", "asdasdasd"] microblog.js:48 

// adding third element;  array length = 3
["asdasdasd", "asdasdasd", "asdasdasdqwe"] microblog.js:48 

// removing second element;  array length = 2
["asdasdasd", "asdasdasdqwe"] microblog.js:66 

// removing third element; array length = 1
["asdasdasdqwe"] microblog.js:66

// adding new element;  array length = 4
["asdasdasdqwe", undefined × 2, "asdasdqwrevcvxzvvxwfrqeqwewq"] 

And this is how I add an element:

(...)
this.list[currId] = text;
(...)

And this is how I remove an element

(...)
this.list.splice(currId, 1);
(...)

How to avoid this undefined elements?

  • 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-06T15:02:46+00:00Added an answer on June 6, 2026 at 3:02 pm

    Either A) -> You’re using current element to track the position to next add to, and never decreasing it:

    var arr = []
    for (var currId = 0; currId < 4; currId++) {
        arr[currId] = currID
    }
    //arr = [1, 2, 3, 4]
    arr.splice(1, 3); // arr = [1]
    // if currID is still 3
    arr[currId] = "text"; // now arr = [1, undefined, undefined, "text"]
    

    in which case you need to decrease currId when you remove items to add them in the very next place.

    Or B) -> just use Array.push()

    var arr = []
    for (var currId = 0; currId < 4; currId++) {
        arr.push(currId);
    }
    //arr = [1, 2, 3, 4]
    arr.splice(1, 3); // arr = [1]
    arr.push("text"); // now arr = [1, "text"]
    

    B is a much better solution, if just adding the element to the end of the array is what you wanted.

    EDIT: Since you want to maintain the “currID” as the next position in the Array to place an item at, I assume whenever you do this,

    this.list[currId] = text;
    

    you follow it with this:

    currId++;
    

    If that is correct, then the opposite thing to do would be to decrease it when you splice objects out of the Array e.g:

    var arr = [1, 2, 3, 4];
    currId = 4 // The next position to add at is 4.
    removeCount = 3;
    arr.splice(1, removeCount); // arr = [1]
    currId -= removeCount;
    

    But frankly, this is an awful way to do this. Please give some more information so that we can help you find a better way.

    Hope this helps.

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

Sidebar

Related Questions

I have strange problem with receiving data from socket. On client im using air
I just got a strange problem. I am including some files from my index.php,
I got a strange problem with split function over the String. Please look at
I'm having a strange problem connecting via remote to MySQL. This is an internal
I was recently tasked with debugging a strange problem within an e-commerce application. After
Strange problem. My Query looks like SELECT DISTINCT ID, `etcetc`, `if/elses over muliple joined
Strange problem occurs on the production platform (64 bit win 2008 server). It is
Got a strange problem created a little stored proc which need to execute a
my problem looks strange. when I enter www.upitr.com in browsers, it works, but for
i have strange problem doing reporting: i have numerous clients with different issued invoices.

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.