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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:37:31+00:00 2026-05-31T08:37:31+00:00

I am dynamically building an array of objects using a process that boils down

  • 0

I am dynamically building an array of objects using a process that boils down to something like this:

//Objects  Array
var objects = [];

//Object Structure
var object1 =  {"id":"foobar_1", "metrics":90};
var object2 = {"id":"some other foobar",  "metrics":50};
objects[0] = object1;
objects[1] = object2;

(Let it be said for the record, that if you can think of a better way to dynamically nest data such that I can access it with objects[i].id I am also all ears!)

There’s ultimately going to be more logic at play than what’s above, but it’s just not written yet. Suffice it to say that the “object1” and “object2” parts will actually be in an iterator.

Inside that iterator, I want to check for the presence of an ID before adding another object to the array. If, for example, I already have an object with the ID "foobar_1", instead of pushing a new member to the array, I simply want to increment its "metrics" value.

If I wasn’t dealing with an array of objects, I could use inArray to look for “foobar_1” (a jQuery utility). But that won’t look into the object’s values. The way I see it, I have two options:

  1. Keep a separate simple array of just the IDs. So instead of only relying on the objects array, I simply check inArray (or plain JS equivalent) for a simple “objectIDs” array that is used only for this purpose.

  2. Iterate through my existing data object and compare my "foobar_1" needle to each objects[i].id haystack

I feel that #1 is certainly more efficient, but I can’t help wondering if I’m missing a function that would do the job for me. A #3, 4, or 5 option that I’ve missed! CPU consumption is somewhat important, but I’m also interested in functions that make the code less verbose whether they’re more cycle-efficient or not.

  • 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-31T08:37:32+00:00Added an answer on May 31, 2026 at 8:37 am

    I’d suggest switching to an object instead of an array:

    var objects = {};
    objects["foobar_1"] = {metrics: 90};
    objects["some other foobar"] = {metrics: 50};
    

    Then, to add a new object uniquely, you would do this:

    function addObject(id, metricsNum) {
        if (!(id in objects)) {
            objects[id] = {metrics: metricsNum};
        } 
    }
    

    To iterate all the objects, you would do this:

    for (var id in objects) {
        // process objects[id]
    }
    

    This gives you very efficient lookup for whether a given id is already in your list or not. The only thing it doesn’t give you that the array gave you before is a specific order of objects because the keys of an object don’t have any specific order.

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

Sidebar

Related Questions

I'm dynamically building a nhibernate projected query that needs to implement paging. Something like...
I'm building a simple list using a dynamically allocated array in C++. I have
I have a stored procedure that is dynamically building a query. The where clause
I have a huge html table that I am building dynamically, and I want
I'm building a quiz web application that dynamically generates from a database on my
I am dynamically building menu's based on an array and there is a sub-menu
I'm currently dynamically building an asp:table which contains checkboxes these checkboxes have a CheckChanged
We are dynamically building some SQL statements and we are utilizing the IN operator.
Is there an easy way of dynamically building a filepath in .Net? At the
I'm trying to evaluate an XPath varable I'm building dynamically based on the position

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.