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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:37:18+00:00 2026-05-25T17:37:18+00:00

Here is what I have currently: var myArray = []; myArray[15] = 2; myArray[6323]

  • 0

Here is what I have currently:

var myArray = [];
myArray[15] = 2;
myArray[6323] = 1;
myArray[349022] = 3;
myArray = myArray.filter(function(i,v,a){return ! (typeof v == 'undefined')});
myArray = myArray.sort(function(a,b){return (a - b)});

When I console.log() allocationOrder after the “filter” function, I get the values that I expect, but the indices are not maintained. How can I maintain the indices but also remove the undefined values (due to having spread out indices)? Keep in mind that I need to sort the array by value afterwards (which is why I avoided objects as a solution).

  • 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-25T17:37:19+00:00Added an answer on May 25, 2026 at 5:37 pm

    You can’t have your cake and eat it too.

    You can’t have a sparse array (only some values filled in) and have no undefined values in between. That’s just not how javascript arrays work. Further, you can remove the elements that have undefined values and expect the other values to stay at the same index. By defintion, removing those other values, collapses the array (changing indexes).

    Perhaps if you can describe what you’re really trying to accomplish we can figure out a better way to approach the problem.

    I’m thinking that what you want is an array of objects where each object in the array has an index and a value. The indexes on the objects in the array will never change, but the container array can be sorted by those indexes.

    var mySortedArray = [
        {index:15, value: 2},
        {index:6323, value: 1},
        {index:349022, value: 3}
    ];
    
    mySortedArray = myArray.sort(function(a, b) {a.value - b.value});
    

    Now you have a sorted array that’s three elements long and it’s sorted by value and the original index is preserved and available.

    The only drawback is that you can’t easily access it by the index. If you wanted to be able to have both ordered, sorted access and access by the index key, you could make a parallel data structure that make key access quick. In Javascript, there is no single data structure that supports both key access and sorted order access.

    If you want to build a parallel data structure that would support fast access by key (and would include the value and the sortOrder value, you could build such an object like this:

    // build a parallel data structure for keyed access
    var myKeyedObject = {}, temp, item;
    for (var i = 0; i < mySortedArray.length; i++) {
        item = mySortedArray[i];
        temp = {};
        temp.value = item.value;
        temp.index = item.index;
        temp.sortOrder = i;
        myKeyedObject[item.index] = temp;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have this function which appends an input to the #cursor-start div: var
I currently have a csv file that I'm parsing with an example from here:
We currently have a form with the standard multi-select functionality of here are the
Here is the thing. I currently have a tabBar controller, with several navigation controllers
Currently i have some interfaces (stripped down for here): public interface IJobGroup { string
I'm not sure if this is even possible but here goes: Currently I have
Ok here is what I am currently trying to do. I have a class
Here's the code I currently have. <!DOCTYPE html> <html> <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8/>
I have these Javascript classes/code: function PageManager () { this.digital_channels = undefined; this.loadConfiguration =
I currently have this code: static void func( uint8_t var ); static volatile uint8_t

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.