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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:18:34+00:00 2026-05-22T22:18:34+00:00

I have some example code: var array = []; array[0] = {id:0}; array[1] =

  • 0

I have some example code:

var array = [];
array[0] = {id:0};
array[1] = {id:1};
array[2] = {id:2};

Now array looks like

[Object{id=0}, Object{id=1}, Object{id=2}] 

I use splice:

array.splice(0,1);

And we have:

[Object{id=1}, Object{id=2}]

When I try for or for ... in length will be only 2 and I can’t iterate it in normal way. Result of loop is:

undefined
Object{id:1}  

In first case (when we use for) I understand why it didn’t work, but for ... in should return indexes 1 and 2 not 0 and 1…

Anybody can explain me what I am doing wrong?

  • 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-22T22:18:35+00:00Added an answer on May 22, 2026 at 10:18 pm

    What did you try? If I issue

    var array = [];
    array[0] = {id:0};
    array[1] = {id:1};
    array[2] = {id:2};
    array.splice(0,1);
    for (var i = 0, len = array.length; i < len; i++)
        console.log(i + ":", array[i]);
    

    the engine correctly outputs

    0: Object { id=1 }
    1: Object { id=2 }
    

    (but the array indices change to 0 and 1, perhaps that is what confuses you; remember that Array.splice by default removes from element 0 onwards and shifts all remaining elements downwards).

    Update: apart from the fact that you can always get the id property of the individual objects using array[i].id, you can delete array elements without shifting the other elements using the delete operator. After I replace array.splice(0,1) with

    delete array[0];
    

    the output shows

    0: undefined
    1: Object { id=1 }
    2: Object { id=2 }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a really basic question but... I have some code like this var
Has anyone done this / have some example code?
So I have some openGL code (such code for example) /* FUNCTION: YCamera ::
I have an example of some code that I see often in websites that
I have some URLs, like http://www.example.com/something?param1=value1&param2=value2&param3=value3 and I would like to extract the parameters
I have, for example, markup like this <div id=content> <p>Here is some wonderful text,
I have code examples from some of my previous work that help me to
I have some .net apps running that I need to monitor for example, then
I have some simple shell scripting tasks that I want to do For example:
I have some null struct, for example: struct null_type { null_type& someNonVirtualMethod() { return

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.