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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:39:12+00:00 2026-06-12T13:39:12+00:00

It should be quite easy to implement array.map() that is defined in ECMA-262 ,

  • 0

It should be quite easy to implement array.map() that is defined in ECMA-262, which takes a function and this function will be called by 3 arguments: element value, index, the array.

But what about for sparse array? Obviously we don’t want to iterate from index 0 to 100,000 if only index 0, 1, 2, and 100,000 has an element and otherwise is sparse from index 3 to 99,999. I can think of using arr.slice(0) or arr.concat() to clone the array, and then put in the replaced values, but what if we don’t use slice or concat, is there another way to do it?

The solution I came up with using slice() is:

Array.prototype.collect = Array.prototype.collect || function(fn) {
    var result = this.slice(0);

    for (var i in this) {
      if (this.hasOwnProperty(i))
        result[i] = fn(this[i], i, this);  // 3 arguments according to ECMA specs
    }
    return result;
};

(collect is used to try out the code, as that’s another name for map in some language)

  • 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-12T13:39:13+00:00Added an answer on June 12, 2026 at 1:39 pm

    It should be easy, but there are a few peculiar points.

    The callback function is allowed to modify the array in question. Any elements it adds or removes are not visited. So it seems we should use something like Object.keys to determine which elements to visit.

    Also, the result is defined to be a new array “created as if by” the array constructor taking the length of the old array, so we might as well use that constructor to create it.

    Here’s an implementation taking these things into account, but probably missing some other subtleties:

    function map(callbackfn, thisArg) {
      var keys = Object.keys(this),
        result = new Array(this.length);
    
      keys.forEach(function(key) {
        if (key >= 0 && this.hasOwnProperty(key)) {
          result[key] = callbackfn.call(thisArg, this[key], key, this);
        }
      }, this);
    
      return result;
    }
    

    I am assuming Object.keys returns the keys of the array in numerical order, which I think is implementation defined. If it doesn’t, you could sort them.

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

Sidebar

Related Questions

This should be quite easy, and I have done some research on this. I
This seems like it should be easy, but I can't quite find an explanation
This basic app which was given to me as an project should run quite
My question is quite simple and with the SharpSvn Api, it should be easy
This should be quite simple, but I can't work out the syntax. I have
This task should be quite simple, but nothing I tried has worked. I'm just
This should reduce the executable size quite a bit in some of my very
The question is quite generic. What are the points that should be kept in
This is a general question which will apply to any WPF control. What I
So, I'm not quite sure how I should structure this in CakePHP to work

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.