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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:20:56+00:00 2026-05-19T12:20:56+00:00

I have a JavaScript array of objects like this: box[0] = {…} box[1] =

  • 0

I have a JavaScript array of objects like this:

box[0] = {...}
box[1] = {...}
box[2] = {...}
...
box[499] = {...}

This objects are generated by the same constructor and added to the array inside a loop. The objects have methods in the prototype which need to know the object’s index in the array to do their stuff. Currently what I am doing is to set a property called id inside each object when I create it inside the loop, equal to the array index. Something like this:

box[i].id = i;

However I am not totally satisfied with this because each time I reorder the array using sort() I have to run a loop to update the id properties with the new index values.

My question is if there is a way to know inside an object its index in the array, without having to set the id property, hope you can help me.

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-05-19T12:20:56+00:00Added an answer on May 19, 2026 at 12:20 pm

    I don’t think a function inside an object in the Array is going to be aware of the index of the Array that is referencing it.

    Because each item in the Array is simply pointing to that object in memory, there could conceivably be dozens of Array items, variables, Object properties, etc that reference that same object, so the function (or the object that contains the function) wouldn’t know which back reference you’re hoping to make.

    I’m guessing you’ll be stuck doing what you’re doing if you need to know its index in the Array.

    I suppose that function could call indexOf() against the Array since it returns an index, but that would require some overhead for each call, and you’ll need to added it for unsupported browsers.

    theArr.indexOf( this ); // assuming the function was called from the context
                            //   of the object in question
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JavaScript array of objects with the same properties each, something like
I have a collection of objects in JavaScript like this: Object collection = new
I have a javascript array of Date objects, which I'd like to test for
I have this function to sort a JavaScript array of objects based on a
I have an array of javascript objects like the following: var food = [
I'm creating my own JavaScript Array-like object and I have methods that call closures.
I have a one-dimensional array of strings in JavaScript that I'd like to turn
I have an JavaScript object like this: id=1; name = serdar; and I have
I have an array of javascript objects and I want to press a submit
in my javascript, i have an Array of Calendar objects. Each Calendar objects has

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.