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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:19:48+00:00 2026-05-27T17:19:48+00:00

I am working on cleaning up some adopted code – there is a lot

  • 0

I am working on cleaning up some adopted code – there is a lot of duplication.

There is a set of jQuery callbacks where we get back JSON like this:

[
{"location":{"id":164,"name":"place 1"},"users":[{"id":1,"name":"joe"},{"id":2,"name":"jack"}]},
{"location":{"id":162,"name":"place 2"},"users":[{"id":3,"name":"joe"},{"id":4,"name":"jon"}]}
]

I go through with these functions:

function locations_view(r) {
    str = "";

    $.each(r.data, function(k, v) {
        str += v.location.name + "<br />";
        iterate_users(v.users);
    });

    $("#locations").html(str);
} 

function iterate_users(users) {
    str += '<strong>users:</strong>' + users.length + '<br />';

    $.each(users, function(k1, v1) {
        str += "<a href='/users/" + v1.id + "'>" + v1.name + "</a> ";   
    });

    str += "<br />";
}

This seems to work but it looks a little ugly. Is there a better way to do this. Also, I want to minimize memory consumption (returning the string rather than have a global str was causing performance issues). Is there a better, more elegant way to do this? Would having multiple copies of str ever cause a problem? Like if I have products_view that also uses a str?

  • 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-27T17:19:48+00:00Added an answer on May 27, 2026 at 5:19 pm

    Yes, if something else uses str in the wrong way (i.e. also globally) then that will cause a problem. Best not to have any globals at all. You could change them to this:

    function locations_view(r) {
        var str = "";
    
        $.each(r.data, function(k, v) {
            str += v.location.name + "<br />";
            str += iterate_users(v.users);
        });
    
        $("#locations").html(str);
    } 
    
    function iterate_users(users) {
        var str = '<strong>users:</strong>' + users.length + '<br />';
    
        $.each(users, function(k1, v1) {
            str += "<a href='/users/" + v1.id + "'>" + v1.name + "</a> ";   
        });
    
        return str + "<br />";
    }
    

    And that would be much better. If there are other functions that use iterate_users, they’ll need to be changed too.

    Also, if you’re not already doing this, consider escaping the HTML in v1.name to prevent script injection. You could also use jQuery/DOM manipulation instead.

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

Sidebar

Related Questions

I'm working on some code that generates a lot of ignoring return value of
I'm working on cleaning up some of my code and I came to a
I am cleaning up some code in a C# app that I wrote and
Hey all, I'm working on cleaning up my code from previous semesters. Previously I
I'm copying some objective c++ files over from another (working) project. I get no
I'm cleaning up someone else's code and working with vim. I'd like to reflow
I'm working on some applications that require very low latency and push a lot
I am working on cleaning up a mess that another programmer started. The created
Working with python interactively, it's sometimes necessary to display a result which is some
It's not code-related but IDE related. I'm working on a .NET solution with about

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.