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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:46:43+00:00 2026-06-07T20:46:43+00:00

Alright, I have the sneaking suspicion that I am overlooking something really simple, but

  • 0

Alright, I have the sneaking suspicion that I am overlooking something really simple, but I am having an issue with the following code:

Array.prototype.sortArr = function (skey)
{
    var vals = new Array();
    for (key in this)
    {
        if (typeof(this[key][skey]) != 'undefined')
        {
            vals.push((this[key][skey]+'').toLowerCase());
        }
    }
    vals.sort();

    var newArr = new Array();
    for (i=0;i<vals.length;i++)
    {
        for (key in this)
        {
            if (typeof(this[key][skey]) != 'undefined')
            {
                if ((this[key][skey]+'').toLowerCase() == vals[i])
                {
                    newArr.push(this[key]);
                    break;
                }
            }
        }
    }
    return newArr;
}

Okay, in a nutshell, this function is similar to the sort function with the exception that it sorts through a multi-level object and I specify which keys it needs to sort by.
The issue is that I never actually replace or alter the ‘this’ value, meaning that this function doesn’t actually do anything. For example:

var arr = new Array(new Array(1,5),new Array(2,0));
arr.sortArr(1);

It will not change the arr from before or after the sortArr(1) call. However, when I put an alert(newArr) right before the return of the function, it shows that it actually did reorder. So, my question is; how do I replace the this value in the callback function or at least return the proper/ new array? Thanks in advance.

  • 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-07T20:46:44+00:00Added an answer on June 7, 2026 at 8:46 pm

    To modify the current array at the end of your method, you would need to assign to its elements like this, copying all the elements into the current object’s array before the end of the function:

    this.length = newArr.length;
    for (var i = 0; i < newArr.length; i++) {
        this[i] = newArr[i];
    }
    

    You could also use .splice() like this:

    newArr.unshift(this.length);     // how many items to remove for .splice()
    newArr.unshift(0);               // index for .splice() operation
    this.splice.apply(this, newArr); // add the newArr elements
    

    FYI, it’s not generally correct to use the for/in construct on arrays because it will iterate properties on the object too in addition to array elements and that will often foul you up. If you want to iterate the items in the array, use the traditional for loop like I did in my code snippet above.

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

Sidebar

Related Questions

Alright I have a question that seems simple but I haven't been able to
Alright, So I have an issue that is a bit weird. I am using
Alright I have an xml document that looks something like this: <xml> <list> <partner>
Alright I know that the .closest() have been discussed before, but I have been
Alright so I have no idea how to even begin doing this But basically
Alright so I have a task, that I have to let the client try
Alright simple question I have integriters like 5 188 4634 And they all need
Alright, so I have this collision detection code, and I'm trying to push the
Alright, I have this program to sparse code in Newick Format, which extracts both
Alright I have two problems. I'm using AVAudioPlayer to play a simple audio file

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.