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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:28:52+00:00 2026-05-13T06:28:52+00:00

Is there a better way other than looping to find data in JSON ?

  • 0

Is there a better way other than looping to find data in JSON? It’s for edit and delete.

for(var k in objJsonResp) {
  if (objJsonResp[k].txtId == id) {
    if (action == 'delete') {
      objJsonResp.splice(k,1);
    } else {
      objJsonResp[k] = newVal;
    }
    break;
  }
}

The data is arranged as list of maps.
Like:

[
  {id:value, pId:value, cId:value,...},
  {id:value, pId:value, cId:value,...},
  ...
]
  • 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-13T06:28:53+00:00Added an answer on May 13, 2026 at 6:28 am

    (You’re not searching through “JSON”, you’re searching through an array — the JSON string has already been deserialized into an object graph, in this case an array.)

    Some options:

    Use an Object Instead of an Array

    If you’re in control of the generation of this thing, does it have to be an array? Because if not, there’s a much simpler way.

    Say this is your original data:

    [
        {"id": "one",   "pId": "foo1", "cId": "bar1"},
        {"id": "two",   "pId": "foo2", "cId": "bar2"},
        {"id": "three", "pId": "foo3", "cId": "bar3"}
    ]
    

    Could you do the following instead?

    {
        "one":   {"pId": "foo1", "cId": "bar1"},
        "two":   {"pId": "foo2", "cId": "bar2"},
        "three": {"pId": "foo3", "cId": "bar3"}
    }
    

    Then finding the relevant entry by ID is trivial:

    id = "one"; // Or whatever
    var entry = objJsonResp[id];
    

    …as is updating it:

    objJsonResp[id] = /* New value */;
    

    …and removing it:

    delete objJsonResp[id];
    

    This takes advantage of the fact that in JavaScript, you can index into an object using a property name as a string — and that string can be a literal, or it can come from a variable as with id above.

    Putting in an ID-to-Index Map

    (Dumb idea, predates the above. Kept for historical reasons.)

    It looks like you need this to be an array, in which case there isn’t really a better way than searching through the array unless you want to put a map on it, which you could do if you have control of the generation of the object. E.g., say you have this originally:

    [
        {"id": "one",   "pId": "foo1", "cId": "bar1"},
        {"id": "two",   "pId": "foo2", "cId": "bar2"},
        {"id": "three", "pId": "foo3", "cId": "bar3"}
    ]
    

    The generating code could provide an id-to-index map:

    {
        "index": {
            "one": 0, "two": 1, "three": 2
        },
        "data": [
            {"id": "one",   "pId": "foo1", "cId": "bar1"},
            {"id": "two",   "pId": "foo2", "cId": "bar2"},
            {"id": "three", "pId": "foo3", "cId": "bar3"}
        ]
    }
    

    Then getting an entry for the id in the variable id is trivial:

    var index = objJsonResp.index[id];
    var obj = objJsonResp.data[index];
    

    This takes advantage of the fact you can index into objects using property names.

    Of course, if you do that, you have to update the map when you modify the array, which could become a maintenance problem.

    But if you’re not in control of the generation of the object, or updating the map of ids-to-indexes is too much code and/ora maintenance issue, then you’ll have to do a brute force search.

    Brute Force Search (corrected)

    Somewhat OT (although you did ask if there was a better way 🙂 ), but your code for looping through an array is incorrect. Details here, but you can’t use for..in to loop through array indexes (or rather, if you do, you have to take special pains to do so); for..in loops through the properties of an object, not the indexes of an array. Your best bet with a non-sparse array (and yours is non-sparse) is a standard old-fashioned loop:

    var k;
    for (k = 0; k < someArray.length; ++k) { /* ... */ }
    

    or

    var k;
    for (k = someArray.length - 1; k >= 0; --k) { /* ... */ }
    

    Whichever you prefer (the latter is not always faster in all implementations, which is counter-intuitive to me, but there we are). (With a sparse array, you might use for..in but again taking special pains to avoid pitfalls; more in the article linked above.)

    Using for..in on an array seems to work in simple cases because arrays have properties for each of their indexes, and their only other default properties (length and their methods) are marked as non-enumerable. But it breaks as soon as you set (or a framework sets) any other properties on the array object (which is perfectly valid; arrays are just objects with a bit of special handling around the length property).

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

Sidebar

Related Questions

I'm wondering if there is a better/inbuilt way, other than using a byte buffer
Is there a better way to flash a window in Java than this: public
Is there a better way to debug JavaScript than MS Script Editor? I am
Is there a better way than simply trying to open the file? int exists(const
Is there a better way to negate a boolean in Java than a simple
Is there a better way to forcefully disconnect all users from an Oracle 10g
Is there a better way of binding a list of base class to a
Is there a better way to do this? -(NSDate *)getMidnightTommorow { NSCalendarDate *now =
Is there a better way to watch for new entries in a table besides
Is there a better way to do the following: $array = array('test1', 'test2', 'test3',

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.