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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:46:12+00:00 2026-05-27T22:46:12+00:00

Possible Duplicate: indexOf method in an object array? I have a javascript array which

  • 0

Possible Duplicate:
indexOf method in an object array?

I have a javascript array which follows this format:

var arrayName = [
{id: "a", gender: "man",   item: "stuff"},
{id: "b", gender: "woman", item: "stuff"},
{id: "c", gender: "man",   item: "stuff"},
{id: "d", gender: "man",   item: "stuff"}
];

Is there a way that I can use array.indexOf to find an index in the array, when for example I know the “id” variable.

For example I tried;

var position = arrayName.indexOf("b");
arrayName[position].gender = "man"

At the moment I am using;

for(var i=0; i<arrayName.length; i++) {
   if(arrayName[i].id == "b"){
       arrayName[i].gender = "man";
   }
}

This second technique works but the actual array I am using has 150 entries and 10 items in each entry so looping through it all seems very wasteful when I know the “id” of the entry I want to edit. indexOf would be a much cleaner approach if I can get it working.

  • 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-27T22:46:13+00:00Added an answer on May 27, 2026 at 10:46 pm

    Your call to indexOf is checking the objects in your array against the string b, which will never work.

    Looping through the array elements to find the right id is a simple solution.

    Or, you could make your own indexOf function:

    Array.prototype.indexOfId = function(id) {
        for (var i = 0; i < this.length; i++)
            if (this[i].id === id)
                return i;
        return -1;
    }
    
        var arrayName = [
          { id: "a", gender: "man", item: "stuff" },
          { id: "b", gender: "woman", item: "stuff" },
          { id: "c", gender: "man", item: "stuff" },
          { id: "d", gender: "man", item: "stuff" }];
    
        var position = arrayName.indexOfId("b");
    
        alert(position);  //alerts 1
    

    Here’s a fiddle

    EDIT

    If you want to compare your array elements to any property, here’s how you do it (note that I’m using [] syntax to get an arbitrary property)

        Array.prototype.indexOfField = function (propertyName, value) {
            for (var i = 0; i < this.length; i++)
                if (this[i][propertyName] === value)
                    return i;
            return -1;
        }
    
        var position = arrayName.indexOfField("id", "b");
        alert(position);  //alerts 1
    
        position = arrayName.indexOfField("gender", "man");
        alert(position); //alerts 0
    

    Or, if you don’t want to mess with Array’s prototype, and you don’t mind using features not present in older browsers, you could use Array’s filter function

    var position = arrayName.indexOf(arrayName.filter(function (val) {
          return val.id === "b";
    })[0]);
    

    Note that this function is not present in IE8, and so to support this browser you’d have to grab the shim from MDN

    EDIT – oops, indexOf isn’t friendly toward old browsers either. If you opt for this second bit of code and wanted to support IE, you’d also have to grab the shim for indexOf from here

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

Sidebar

Related Questions

Possible Duplicate: substring and the indexOf method I have this problem that I'm going
Possible Duplicate: Find object by id in an array of JavaScript objects How to
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: Why does ReSharper want to use 'var' for everything? I have ReSharper
Possible Duplicate: Validate email address in Javascript? I have javascript for email validation :
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Java GUI repaint() problem? I write a Java code, but I have

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.