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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:08:49+00:00 2026-05-24T20:08:49+00:00

As for now, when I prepare my data to be sent by Ajax request

  • 0

As for now, when I prepare my data to be sent by Ajax request to my web app, I just concat my JS arrays (with placing -1 between them as separator – values can be positive only, so -1 means start of new array). This seems a bit ugly for me, so I’m wondering what would be best practice to refator this.

    var online1 = [];
    var online2 = [];
    var online3 = [];
    var online4 = [];

    for(i = 0 ; i < listOfPlayers.length ; i++) {

        var player = listOfPlayers[i].href;             
        var uid = player.substring(player.lastIndexOf('=') + 1);

        if(onlineStatus[i].className == "online1"){
            online1.push(uid);
        }   
        if(onlineStatus[i].className == "online2"){
            online2.push(uid);
        }   
        if(onlineStatus[i].className == "online3"){
            online3.push(uid);
        }   
        if(onlineStatus[i].className == "online4"){
            online4.push(uid);
        }
    }

    online1.push(-1);
    online2.push(-1);
    online3.push(-1);
    online4.push(-1);
    var result = online1.concat(online2, online3, online4);

    //...
    ajaxRequest.send("result="+result);
  • 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-24T20:08:51+00:00Added an answer on May 24, 2026 at 8:08 pm

    You could do two things:

    1. Use an object, stringify it using JSON.stringify. You can parse it using JSON.parse, even server-side solutions exist. JSON is available in recent browsers and as library.

    2. Make the if generic.

    E.g.:

    var online = {1: [],
                  2: [],
                  3: [],
                  4: []};
    
    for(i = 0 ; i < listOfPlayers.length ; i++) {
    
        var player = listOfPlayers[i].href;             
        var uid = player.substring(player.lastIndexOf('=') + 1);
        var number = onlineStatus[i].className.substring(6);
    
        online[number].push(uid);
    }
    
    var result = JSON.stringify(online);
    
    //...
    ajaxRequest.send("result="+result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

now i need to insert some data from the sqlserver into a word,i know
I have a requirement where i need to prepare data for email ,So i
I have a PHP script that extract valid GeoJSON data from a database. Now
I have never designed a database/data-model/schema from scratch, especially for a web-application. In some
I'm developing simple Windows Service sending and receiving data from remote web service. I
I'm still implementing basic functionality in my app so I'm asking this now in
I've built a first-run web service on Zend Framework (1.10), and now I'm looking
I am using the Cart class of CodeIgniter, basically that's just sessions. Now, Safari
In most cases when you create your thread you can prepare the data beforehand
My page sends using ajax data to a php script. one of the vars

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.